RenderUnitView.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. //
  2. // SelfRenderView.swift
  3. // RoderickRalph
  4. //
  5. // Created by Neoa on 2025/1/11.
  6. //
  7. import UIKit
  8. import AnyThinkNative
  9. // 定义常量(混淆命名)
  10. let kNRPaneW = UIScreen.main.bounds.width
  11. let kNRPaneH: CGFloat = 170
  12. let kNRMediaW = UIScreen.main.bounds.width
  13. let kNRMediaH: CGFloat = 160
  14. class RenderUnitView: UIView {
  15. // MARK: - UI Components
  16. /// 广告主
  17. var advLabel: UILabel!
  18. /// 内容
  19. var bodyLabel: UILabel!
  20. /// 标题
  21. var headlineLabel: UILabel!
  22. /// 类似"点我下载"按钮上面的口号
  23. var actionLabel: UILabel!
  24. /// 评分
  25. var scoreLabel: UILabel!
  26. /// 推广应用icon
  27. var badgeImageView: UIImageView!
  28. /// 主图
  29. var heroImageView: UIImageView!
  30. /// 广告平台logo标识
  31. var brandLogoView: UIImageView!
  32. /// 关闭按钮
  33. var closeControl: UIButton!
  34. /// 媒体视图
  35. var mediaContainer: UIView?
  36. // only for yandex native
  37. var hostLabel: UILabel!
  38. var alertLabel: UILabel!
  39. // MARK: - Private Properties
  40. private var adPayload: ATNativeAdOffer?
  41. // MARK: - Initialization
  42. override init(frame: CGRect) {
  43. super.init(frame: frame)
  44. configureSurface()
  45. }
  46. required init?(coder: NSCoder) {
  47. super.init(coder: coder)
  48. configureSurface()
  49. }
  50. convenience init(offer: ATNativeAdOffer) {
  51. self.init(frame: .zero)
  52. self.adPayload = offer
  53. configureSurface()
  54. }
  55. deinit {
  56. print("🔥---RenderUnitView--dealloc")
  57. }
  58. // MARK: - UI Setup
  59. private func configureSurface() {
  60. // 随机背景色
  61. backgroundColor = makeRandomTint()
  62. assembleSubviews()
  63. constrainSubviews()
  64. applyOfferContent()
  65. }
  66. private func assembleSubviews() {
  67. // // 广告主标签
  68. // advertiserLabel = UILabel()
  69. // advertiserLabel.font = UIFont.boldSystemFont(ofSize: 15.0)
  70. // advertiserLabel.textColor = .black
  71. // advertiserLabel.textAlignment = .left
  72. // advertiserLabel.translatesAutoresizingMaskIntoConstraints = false
  73. // addSubview(advertiserLabel)
  74. // 标题标签
  75. headlineLabel = UILabel()
  76. headlineLabel.font = UIFont.systemFont(ofSize: 14.0)
  77. headlineLabel.textColor = .gray
  78. headlineLabel.textAlignment = .left
  79. headlineLabel.translatesAutoresizingMaskIntoConstraints = false
  80. addSubview(headlineLabel)
  81. // 内容标签
  82. bodyLabel = UILabel()
  83. bodyLabel.font = UIFont.boldSystemFont(ofSize: 16.0)
  84. bodyLabel.textColor = .black
  85. bodyLabel.numberOfLines = 0
  86. bodyLabel.translatesAutoresizingMaskIntoConstraints = false
  87. addSubview(bodyLabel)
  88. // // CTA标签
  89. // ctaLabel = UILabel()
  90. // ctaLabel.font = UIFont.systemFont(ofSize: 15.0)
  91. // ctaLabel.textColor = .white
  92. // ctaLabel.backgroundColor = .blue
  93. // ctaLabel.textAlignment = .center
  94. // ctaLabel.translatesAutoresizingMaskIntoConstraints = false
  95. // addSubview(ctaLabel)
  96. //
  97. // // 评分标签
  98. // ratingLabel = UILabel()
  99. // ratingLabel.font = UIFont.systemFont(ofSize: 15.0)
  100. // ratingLabel.textColor = .systemYellow
  101. // ratingLabel.translatesAutoresizingMaskIntoConstraints = false
  102. // addSubview(ratingLabel)
  103. // // 域名标签 (yandex)
  104. // domainLabel = UILabel()
  105. // domainLabel.font = UIFont.systemFont(ofSize: 15.0)
  106. // domainLabel.textColor = .black
  107. // domainLabel.translatesAutoresizingMaskIntoConstraints = false
  108. // addSubview(domainLabel)
  109. //
  110. // // 警告标签 (yandex)
  111. // warningLabel = UILabel()
  112. // warningLabel.font = UIFont.systemFont(ofSize: 15.0)
  113. // warningLabel.textColor = .black
  114. // warningLabel.translatesAutoresizingMaskIntoConstraints = false
  115. // addSubview(warningLabel)
  116. // // 图标
  117. // iconImageView = UIImageView()
  118. // iconImageView.layer.cornerRadius = 4.0
  119. // iconImageView.layer.masksToBounds = true
  120. // iconImageView.contentMode = .scaleAspectFit
  121. // iconImageView.translatesAutoresizingMaskIntoConstraints = false
  122. // addSubview(iconImageView)
  123. // 主图
  124. heroImageView = UIImageView()
  125. heroImageView.contentMode = .scaleAspectFit
  126. heroImageView.translatesAutoresizingMaskIntoConstraints = false
  127. addSubview(heroImageView)
  128. // Logo
  129. // logoImageView = UIImageView()
  130. // logoImageView.contentMode = .scaleAspectFit
  131. // logoImageView.tag = 110220
  132. // logoImageView.translatesAutoresizingMaskIntoConstraints = false
  133. // addSubview(logoImageView)
  134. // 关闭按钮
  135. closeControl = UIButton(type: .custom)
  136. closeControl.backgroundColor = .white
  137. // 尝试从AnyThinkSDK bundle中获取关闭按钮图片
  138. // if let bundlePath = Bundle.main.path(forResource: "AnyThinkSDK", ofType: "bundle"),
  139. // let bundle = Bundle(path: bundlePath),
  140. // let closeImg = UIImage(named: "offer_video_close", in: bundle, compatibleWith: nil) {
  141. // dislikeButton.setImage(closeImg, for: .normal)
  142. // } else {
  143. closeControl.setTitle("×", for: .normal)
  144. closeControl.setTitleColor(.gray, for: .normal)
  145. closeControl.titleLabel?.font = UIFont.boldSystemFont(ofSize: 18)
  146. // }
  147. closeControl.translatesAutoresizingMaskIntoConstraints = false
  148. addSubview(closeControl)
  149. enableInteractions()
  150. }
  151. private func enableInteractions() {
  152. // ctaLabel.isUserInteractionEnabled = true
  153. // advertiserLabel.isUserInteractionEnabled = true
  154. headlineLabel.isUserInteractionEnabled = true
  155. bodyLabel.isUserInteractionEnabled = true
  156. // ratingLabel.isUserInteractionEnabled = true
  157. // domainLabel.isUserInteractionEnabled = true
  158. // warningLabel.isUserInteractionEnabled = true
  159. // iconImageView.isUserInteractionEnabled = true
  160. heroImageView.isUserInteractionEnabled = true
  161. // logoImageView.isUserInteractionEnabled = true
  162. }
  163. private func applyOfferContent() {
  164. guard let offer = adPayload else { return }
  165. // 设置图标
  166. // if let icon = offer.nativeAd.icon {
  167. // iconImageView.image = icon
  168. // } else if let iconUrl = offer.nativeAd.iconUrl, let url = URL(string: iconUrl) {
  169. // loadImage(from: url, into: iconImageView)
  170. // print("🔥AnyThinkDemo::iconUrl:\(iconUrl)")
  171. // }
  172. // 设置主图
  173. if let mainImage = offer.nativeAd.mainImage {
  174. heroImageView.image = mainImage
  175. } else if let imageUrl = offer.nativeAd.imageUrl, let url = URL(string: imageUrl) {
  176. fetchImage(from: url, into: heroImageView)
  177. print("🔥AnyThinkDemo::imageUrl:\(imageUrl)")
  178. }
  179. // // 设置Logo
  180. // if let logoUrl = offer.nativeAd.logoUrl, !logoUrl.isEmpty {
  181. // print("🔥----logoUrl:\(logoUrl)")
  182. // if let url = URL(string: logoUrl) {
  183. // loadImage(from: url, into: logoImageView)
  184. // }
  185. // } else if let logo = offer.nativeAd.logo {
  186. // print("🔥----logo:\(logo)")
  187. // logoImageView.image = logo
  188. // } else {
  189. // // 根据networkFirmID设置默认平台logo
  190. //// if offer.networkFirmID == ATNetworkFirmIDTypeCSJ {
  191. //// logoImageView.image = UIImage(named: "tt_ad_logo_new")
  192. //// }
  193. // }
  194. // 设置文本内容
  195. // advertiserLabel.text = offer.nativeAd.advertiser
  196. headlineLabel.text = offer.nativeAd.title
  197. bodyLabel.text = offer.nativeAd.mainText
  198. // ctaLabel.text = offer.nativeAd.ctaText ?? "查看详情"// : offer.nativeAd.ctaText
  199. // ratingLabel.text = "评分:\(offer.nativeAd.rating?.stringValue ?? "暂无评分")"
  200. // yandex平台支持
  201. // domainLabel.text = offer.nativeAd.domain
  202. // warningLabel.text = offer.nativeAd.warning
  203. print("🔥AnythinkDemo::native domain:\(offer.nativeAd.domain ?? ""); warning:\(offer.nativeAd.warning ?? "")")
  204. print("🔥AnythinkDemo::native文本内容title:\(offer.nativeAd.title ?? ""); text:\(offer.nativeAd.mainText ?? ""); cta:\(offer.nativeAd.ctaText ?? "")")
  205. }
  206. private func constrainSubviews() {
  207. backgroundColor = .white
  208. headlineLabel.backgroundColor = .white
  209. bodyLabel.backgroundColor = .white
  210. NSLayoutConstraint.activate([
  211. // 主图约束
  212. heroImageView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 12),
  213. // mainImageView.centerYAnchor.constraint(equalTo: centerYAnchor),
  214. heroImageView.topAnchor.constraint(equalTo: topAnchor, constant: 10),
  215. heroImageView.widthAnchor.constraint(equalToConstant: 200),
  216. heroImageView.heightAnchor.constraint(equalToConstant: 150),
  217. // // Logo约束
  218. // logoImageView.heightAnchor.constraint(equalToConstant: 25),
  219. // logoImageView.widthAnchor.constraint(equalToConstant: 25),
  220. // logoImageView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -5),
  221. // logoImageView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 5),
  222. // 内容约束
  223. bodyLabel.leadingAnchor.constraint(equalTo: heroImageView.trailingAnchor, constant: 8),
  224. bodyLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -30),
  225. // textLabel.heightAnchor.constraint(equalToConstant: 20),
  226. bodyLabel.topAnchor.constraint(equalTo: heroImageView.topAnchor, constant: 10),
  227. // 标题约束
  228. headlineLabel.leadingAnchor.constraint(equalTo: heroImageView.trailingAnchor, constant: 8),
  229. headlineLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -36),
  230. headlineLabel.heightAnchor.constraint(equalToConstant: 20),
  231. headlineLabel.bottomAnchor.constraint(equalTo: heroImageView.bottomAnchor,constant: 0),
  232. // // CTA约束
  233. // ctaLabel.topAnchor.constraint(equalTo: textLabel.bottomAnchor, constant: 5),
  234. // ctaLabel.widthAnchor.constraint(equalToConstant: 100),
  235. // ctaLabel.heightAnchor.constraint(equalToConstant: ctaLabel.font.lineHeight),
  236. // ctaLabel.leadingAnchor.constraint(equalTo: textLabel.leadingAnchor),
  237. // // 评分约束
  238. // ratingLabel.leadingAnchor.constraint(equalTo: ctaLabel.trailingAnchor, constant: 20),
  239. // ratingLabel.heightAnchor.constraint(equalToConstant: ratingLabel.font.lineHeight),
  240. // ratingLabel.topAnchor.constraint(equalTo: ctaLabel.topAnchor),
  241. //
  242. // // 域名约束
  243. // domainLabel.leadingAnchor.constraint(equalTo: ratingLabel.trailingAnchor, constant: 20),
  244. // domainLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -40),
  245. // domainLabel.topAnchor.constraint(equalTo: ctaLabel.topAnchor),
  246. // domainLabel.heightAnchor.constraint(equalToConstant: 40),
  247. //
  248. // // 广告主约束
  249. // advertiserLabel.heightAnchor.constraint(equalToConstant: advertiserLabel.font.lineHeight),
  250. // advertiserLabel.leadingAnchor.constraint(equalTo: ctaLabel.leadingAnchor),
  251. // advertiserLabel.topAnchor.constraint(equalTo: ctaLabel.bottomAnchor, constant: 5),
  252. //
  253. // // 图标约束
  254. // iconImageView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 20),
  255. // iconImageView.heightAnchor.constraint(equalToConstant: 75),
  256. // iconImageView.widthAnchor.constraint(equalToConstant: 75),
  257. // iconImageView.topAnchor.constraint(equalTo: topAnchor, constant: 20),
  258. //
  259. //
  260. // // 警告标签约束
  261. // warningLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 5),
  262. // warningLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -5),
  263. // warningLabel.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -5),
  264. // warningLabel.heightAnchor.constraint(equalToConstant: 40),
  265. // 关闭按钮约束
  266. closeControl.heightAnchor.constraint(equalToConstant: 20),
  267. closeControl.widthAnchor.constraint(equalToConstant: 20),
  268. closeControl.topAnchor.constraint(equalTo: topAnchor, constant: 5),
  269. closeControl.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -10)
  270. ])
  271. }
  272. // MARK: - Media View
  273. func installMediaView(_ mediaView: UIView?) {
  274. self.mediaContainer = mediaView
  275. if let mediaView = mediaView {
  276. addSubview(mediaView)
  277. mediaView.translatesAutoresizingMaskIntoConstraints = false
  278. // 给mediaView设置约束,与mainImageView相同
  279. NSLayoutConstraint.activate([
  280. mediaView.leadingAnchor.constraint(equalTo: heroImageView.leadingAnchor),
  281. mediaView.trailingAnchor.constraint(equalTo: heroImageView.trailingAnchor),
  282. mediaView.topAnchor.constraint(equalTo: heroImageView.topAnchor),
  283. mediaView.bottomAnchor.constraint(equalTo: heroImageView.bottomAnchor)
  284. ])
  285. }
  286. }
  287. // MARK: - Helper Methods
  288. private func makeRandomTint() -> UIColor {
  289. let r = CGFloat.random(in: 0...255)
  290. let g = CGFloat.random(in: 0...255)
  291. let b = CGFloat.random(in: 0...255)
  292. let a = CGFloat.random(in: 0...255)
  293. return UIColor(red: r/255.0, green: g/255.0, blue: b/255.0, alpha: a/255.0)
  294. }
  295. private func fetchImage(from url: URL, into imageView: UIImageView) {
  296. URLSession.shared.dataTask(with: url) { data, _, _ in
  297. guard let data = data, let image = UIImage(data: data) else { return }
  298. DispatchQueue.main.async {
  299. imageView.image = image
  300. }
  301. }.resume()
  302. }
  303. // MARK: - Cleanup
  304. func teardown() {
  305. // 及时销毁offer
  306. adPayload = nil
  307. mediaContainer?.removeFromSuperview()
  308. mediaContainer = nil
  309. }
  310. }