NewControlPackage.m 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. //
  2. // NewControlPackage.m
  3. // XPH
  4. //
  5. // Created by 罗云飞 on 16/7/7.
  6. // Copyright © 2016年 YAY. All rights reserved.
  7. //
  8. #import "NewControlPackage.h"
  9. @implementation NewControlPackage
  10. #pragma mark - UIButton按钮
  11. +(UIButton *)buttonInitWithTitle:(NSString *)title Frame:(CGRect)frame backgroundImage:(UIImage *)backgroundImage backgroundImageHighlighted:(UIImage *)backgroundImageHighlighted backgroundColor:(UIColor *)backgroundColor textColor:(UIColor *)textColor textAlignment:(UIControlContentHorizontalAlignment)textAlignment font:(UIFont *)font tag:(int)tag target:(id)target action:(SEL)action hidden:(BOOL)hidden userInteractionEnabled:(BOOL)userInteractionEnabled
  12. {
  13. UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
  14. [btn setFrame:frame];
  15. [btn setUserInteractionEnabled:userInteractionEnabled];
  16. [btn setTag:tag];
  17. [btn setTitle:title forState:UIControlStateNormal];
  18. [btn.titleLabel setFont:font];
  19. [btn setTitleColor:textColor forState:UIControlStateNormal];
  20. [btn setContentHorizontalAlignment:textAlignment];
  21. [btn setBackgroundColor:backgroundColor];
  22. [btn setBackgroundImage:backgroundImage forState:UIControlStateNormal];
  23. [btn setBackgroundImage:backgroundImageHighlighted forState:UIControlStateHighlighted];
  24. [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
  25. [btn setHidden:hidden];
  26. return btn;
  27. }
  28. #pragma mark - UILabel标签
  29. +(UILabel *)labelInitWithFrame:(CGRect)frame text:(NSString *)text numberOfLines:(int)numberOfLines backgroundColor:(UIColor *)backgroundColor textColor:(UIColor *)textColor textAlignment:(NSTextAlignment)textAlignment font:(UIFont *)font tag:(int)tag hidden:(BOOL)hidden adjustsFontSizeToFitWidth:(BOOL)adjustsFontSizeToFitWidth userInteractionEnabled:(BOOL)userInteractionEnabled
  30. {
  31. UILabel *label = [[UILabel alloc]initWithFrame:frame];
  32. [label setNumberOfLines:numberOfLines];
  33. [label setBackgroundColor:backgroundColor];
  34. [label setText:text];
  35. [label setTextColor:textColor];
  36. [label setTextAlignment:textAlignment];
  37. [label setFont:font];
  38. [label setTag:tag];
  39. [label setHidden:hidden];
  40. [label setAdjustsFontSizeToFitWidth:adjustsFontSizeToFitWidth];
  41. [label setUserInteractionEnabled:userInteractionEnabled];
  42. return label;
  43. }
  44. #pragma mark - UIScrollView
  45. +(UIScrollView *)scrollViewInitWithFrame:(CGRect)frame scrollEnabled:(BOOL)scrollEnabled contentSize:(CGSize)contentSize backgroundColor:(UIColor *)backgroundColor showsHorizontalScrollIndicator:(BOOL)showsHorizontalScrollIndicator showsVerticalScrollIndicator:(BOOL)showsVerticalScrollIndicator pagingEnabled:(BOOL)pagingEnabled tag:(int)tag hidden:(BOOL)hidden userInteractionEnabled:(BOOL)userInteractionEnabled
  46. {
  47. UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:frame];
  48. [scrollView setScrollEnabled:scrollEnabled];
  49. [scrollView setContentSize:contentSize];
  50. [scrollView setBackgroundColor:backgroundColor];
  51. [scrollView setShowsHorizontalScrollIndicator:showsHorizontalScrollIndicator];
  52. [scrollView setShowsVerticalScrollIndicator:showsVerticalScrollIndicator];
  53. [scrollView setPagingEnabled:pagingEnabled];
  54. [scrollView setTag:tag];
  55. [scrollView setHidden:hidden];
  56. [scrollView setUserInteractionEnabled:userInteractionEnabled];
  57. return scrollView;
  58. }
  59. #pragma mark - UIImageView
  60. +(UIImageView *)imageViewInitWithFrame:(CGRect)frame image:(UIImage *)image highlightedImage:(UIImage *)highlightedImage backgroundColor:(UIColor *)backgroundColor tag:(int)tag hidden:(BOOL)hidden userInteractionEnabled:(BOOL)userInteractionEnabled
  61. {
  62. UIImageView *imageView = [[UIImageView alloc]initWithFrame:frame];
  63. [imageView setImage:image];
  64. [imageView setHighlightedImage:highlightedImage];
  65. [imageView setBackgroundColor:backgroundColor];
  66. [imageView setTag:tag];
  67. [imageView setHidden:hidden];
  68. [imageView setUserInteractionEnabled:userInteractionEnabled];
  69. return imageView;
  70. }
  71. #pragma mark - UITextField
  72. +(UITextField *)textFieldInitWithFrame:(CGRect)frame backgroundImage:(UIImage *)backgroundImage backgroundColor:(UIColor *)backgroundColor textColor:(UIColor *)textColor placeholder:(NSString *)placeholder hidden:(BOOL)hidden tag:(int)tag font:(UIFont *)font textAlignment:(NSTextAlignment)textAlignment clearButtonMode:(UITextFieldViewMode)clearButtonMode clearsOnBeginEditing:(BOOL)clearsOnBeginEditing adjustsFontSizeToFitWidth:(BOOL)adjustsFontSizeToFitWidth secureTextEntry:(BOOL)secureTextEntry keyboardType:(UIKeyboardType)keyboardType returnKeyType:(UIReturnKeyType)returnKeyType userInteractionEnabled:(BOOL)userInteractionEnabled
  73. {
  74. UITextField *textField = [[UITextField alloc]initWithFrame:frame];
  75. [textField setBackground:backgroundImage];
  76. [textField setBackgroundColor:backgroundColor];
  77. [textField setTextColor:textColor];
  78. [textField setPlaceholder:placeholder];
  79. [textField setHidden:hidden];
  80. [textField setTag:tag];
  81. [textField setFont:font];
  82. [textField setTextAlignment:textAlignment];
  83. [textField setClearButtonMode:clearButtonMode];
  84. [textField setClearsOnBeginEditing:clearsOnBeginEditing];
  85. [textField setAdjustsFontSizeToFitWidth:adjustsFontSizeToFitWidth];
  86. [textField setSecureTextEntry:secureTextEntry];
  87. [textField setKeyboardType:keyboardType];
  88. [textField setReturnKeyType:returnKeyType];
  89. [textField setUserInteractionEnabled:userInteractionEnabled];
  90. return textField;
  91. }
  92. #pragma mark - UISearchBar
  93. +(UISearchBar *)searchBarInitWithFrame:(CGRect)frame barStyle:(UIBarStyle)barStyle delegate:(id)delegate backgroundImage:(UIImage *)backgroundImage backgroundColor:(UIColor *)backgroundColor tintColor:(UIColor *)tintColor placeholder:(NSString *)placeholder hidden:(BOOL)hidden tag:(int)tag showsCancelButton:(BOOL)showsCancelButton keyboardType:(UIKeyboardType)keyboardType returnKeyType:(UIReturnKeyType)returnKeyType userInteractionEnabled:(BOOL)userInteractionEnabled
  94. {
  95. UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:frame];
  96. [searchBar setBarStyle:barStyle];
  97. [searchBar setDelegate:delegate];
  98. [searchBar setBackgroundImage:backgroundImage];
  99. [searchBar setBackgroundColor:backgroundColor];
  100. [searchBar setTintColor:tintColor];
  101. [searchBar setPlaceholder:placeholder];
  102. [searchBar setHidden:hidden];
  103. [searchBar setTag:tag];
  104. [searchBar setShowsCancelButton:showsCancelButton];
  105. [searchBar setKeyboardType:keyboardType];
  106. [searchBar setReturnKeyType:returnKeyType];
  107. [searchBar setUserInteractionEnabled:userInteractionEnabled];
  108. return searchBar;
  109. }
  110. #pragma mark - 自定义 输入框底图1
  111. +(UIImageView *)inputBoxInitWithFrame:(CGRect)frame lconImage:(UIImage *)lconImage backgroundImage:(UIImage *)backgroundImage backgroundColor:(UIColor *)backgroundColor hidden:(BOOL)hidden tag:(int)tag userInteractionEnabled:(BOOL)userInteractionEnabled
  112. {
  113. UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
  114. [imageView setUserInteractionEnabled:YES];
  115. [imageView setImage:backgroundImage];
  116. UIImageView *lcon = [[UIImageView alloc]initWithFrame:CGRectMake(12, (frame.size.height-20)/2, 14, frame.size.height-(frame.size.height-20))];
  117. [lcon setImage:lconImage];
  118. [imageView addSubview:lcon];
  119. return imageView;
  120. }
  121. #pragma mark - 自定义 输入框底图2
  122. +(UIView *)customView1InitWithFrame:(CGRect)frame text:(NSString *)text textColor:(UIColor *)textColor textAlignment:(NSTextAlignment)textAlignment font:(UIFont *)font backgroundColor:(UIColor *)backgroundColor hidden:(BOOL)hidden lineHidden:(BOOL)lineHidden tag:(int)tag userInteractionEnabled:(BOOL)userInteractionEnabled
  123. {
  124. UIView *view = [[UIView alloc]initWithFrame:frame];
  125. [view setBackgroundColor:backgroundColor];
  126. [view setHidden:hidden];
  127. [view setTag:tag];
  128. [view setUserInteractionEnabled:userInteractionEnabled];
  129. UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, 65, frame.size.height)];
  130. [label setBackgroundColor:backgroundColor];
  131. [label setText:text];
  132. [label setTextColor:textColor];
  133. [label setTextAlignment:textAlignment];
  134. [label setFont:font];
  135. [view addSubview:label];
  136. UIImageView *lineImageView = [[UIImageView alloc]initWithFrame:CGRectMake(15, frame.size.height-.6, frame.size.width-15, .6)];
  137. [lineImageView setBackgroundColor:NewCellLineColor];
  138. [lineImageView setHidden:lineHidden];
  139. [view addSubview:lineImageView];
  140. return view;
  141. }
  142. #pragma mark - 自定义 协议规则框
  143. +(UIView *)protocolRuleBarInitWithFrame:(CGRect)frame title:(NSString *)title textColor:(UIColor *)textColor textAlignment:(UIControlContentHorizontalAlignment)textAlignment font:(CGFloat)font backgroundColor:(UIColor *)backgroundColor backgroundImage:(UIImage *)backgroundImage backgroundImageSelected:(UIImage *)backgroundImageSelected target:(id)target action:(SEL)action hidden:(BOOL)hidden agreeBTTag:(int)agreeBTTag agreementBTTag:(int)agreementBTTag userInteractionEnabled:(BOOL)userInteractionEnabled
  144. {
  145. UIView *view = [[UIView alloc]initWithFrame:frame];
  146. [view setBackgroundColor:backgroundColor];
  147. [view setHidden:hidden];
  148. [view setUserInteractionEnabled:userInteractionEnabled];
  149. UIButton *agreeBT = [UIButton buttonWithType:UIButtonTypeCustom];
  150. agreeBT.frame = CGRectMake(20, (frame.size.height-16)/2, 16, 16);
  151. [agreeBT setImage:backgroundImageSelected forState:UIControlStateNormal];
  152. [agreeBT setImage:backgroundImage forState:UIControlStateSelected];
  153. [agreeBT addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
  154. [agreeBT setTag:agreeBTTag];
  155. [view addSubview:agreeBT];
  156. CGFloat width = [NewUtils heightforString:[NSString stringWithFormat:@"%@",title] andHeight:20 fontSize:font];
  157. UIButton *agreement = [UIButton buttonWithType:UIButtonTypeCustom];
  158. agreement.frame = CGRectMake(43, (frame.size.height-20)/2, width+10, 20);
  159. [agreement setTitle:title forState:UIControlStateNormal];
  160. [agreement setTitleColor:textColor forState:UIControlStateNormal];
  161. [agreement.titleLabel setFont:[UIFont systemFontOfSize:font]];
  162. [agreement setContentHorizontalAlignment:textAlignment];
  163. [agreement addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
  164. [agreement setTag:agreementBTTag];
  165. [view addSubview:agreement];
  166. return view;
  167. }
  168. #pragma mark - UISwitch
  169. +(UISwitch *)switchInitWithFrame:(CGRect)frame isOn:(BOOL)on hidden:(BOOL)hidden tag:(int)tag userInteractionEnabled:(BOOL)userInteractionEnabled
  170. {
  171. UISwitch *isSwitch = [[UISwitch alloc]initWithFrame:frame];
  172. [isSwitch setOn:on];
  173. [isSwitch setHidden:hidden];
  174. [isSwitch setTag:tag];
  175. [isSwitch setUserInteractionEnabled:userInteractionEnabled];
  176. //[isSwitch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
  177. return isSwitch;
  178. }
  179. #pragma mark - UIView
  180. +(UIView *)viewInitWithFrame:(CGRect)frame backgroundColor:(UIColor *)backgroundColor hidden:(BOOL)hidden tag:(int)tag userInteractionEnabled:(BOOL)userInteractionEnabled
  181. {
  182. UIView *view = [[UIView alloc]initWithFrame:frame];
  183. [view setBackgroundColor:backgroundColor];
  184. [view setHidden:hidden];
  185. [view setTag:tag];
  186. [view setUserInteractionEnabled:userInteractionEnabled];
  187. return view;
  188. }
  189. #pragma mark - UITextView
  190. +(UITextView *)textViewInitWithFrame:(CGRect)frame scrollEnabled:(BOOL)scrollEnabled backgroundColor:(UIColor *)backgroundColor textColor:(UIColor *)textColor text:(NSString *)text font:(UIFont *)font textAlignment:(NSTextAlignment)textAlignment autoresizingMask:(UIViewAutoresizing)autoresizingMask keyboardType:(UIKeyboardType)keyboardType returnKeyType:(UIReturnKeyType)returnKeyType hidden:(BOOL)hidden tag:(int)tag userInteractionEnabled:(BOOL)userInteractionEnabled
  191. {
  192. UITextView *textView = [[UITextView alloc]initWithFrame:frame];
  193. [textView setScrollEnabled:scrollEnabled];
  194. [textView setBackgroundColor:backgroundColor];
  195. [textView setTextColor:textColor];
  196. [textView setText:text];
  197. [textView setTextAlignment:textAlignment];
  198. [textView setFont:font];
  199. [textView setAutoresizingMask:autoresizingMask];//自适应高度
  200. [textView setKeyboardType:keyboardType];
  201. [textView setReturnKeyType:returnKeyType];
  202. [textView setHidden:hidden];
  203. [textView setTag:tag];
  204. [textView setUserInteractionEnabled:userInteractionEnabled];
  205. return textView;
  206. }
  207. #pragma mark - UITableView
  208. +(UITableView *)tableViewInitWithFrame:(CGRect)frame backgroundColor:(UIColor *)backgroundColor style:(NSInteger)style delegate:(id)delegate dataSource:(id)dataSource showsHorizontalScrollIndicator:(BOOL)showsHorizontalScrollIndicator showsVerticalScrollIndicator:(BOOL)showsVerticalScrollIndicator hidden:(BOOL)hidden tag:(int)tag userInteractionEnabled:(BOOL)userInteractionEnabled
  209. {
  210. UITableView *tableView = [[UITableView alloc]initWithFrame:frame style:style];
  211. [tableView setDelegate:delegate];
  212. [tableView setDataSource:dataSource];
  213. [tableView setBackgroundColor:backgroundColor];
  214. [tableView setHidden:hidden];
  215. [tableView setTag:tag];
  216. [tableView setUserInteractionEnabled:userInteractionEnabled];
  217. [tableView setShowsHorizontalScrollIndicator:showsHorizontalScrollIndicator];
  218. [tableView setShowsVerticalScrollIndicator:showsVerticalScrollIndicator];
  219. return tableView;
  220. }
  221. #pragma mark - UICollectionView
  222. +(UICollectionView *)collectionViewInitWithFrame:(CGRect)frame delegate:(id)delegate dataSource:(id)dataSource backgroundColor:(UIColor *)backgroundColor scrollEnabled:(BOOL)scrollEnabled alwaysBounceVertical:(BOOL)alwaysBounceVertical alwaysBounceHorizontal:(BOOL)alwaysBounceHorizontal showsHorizontalScrollIndicator:(BOOL)showsHorizontalScrollIndicator showsVerticalScrollIndicator:(BOOL)showsVerticalScrollIndicator collectionViewFlowLayout:(UICollectionViewFlowLayout *)collectionViewFlowLayout sectionInset:(UIEdgeInsets)sectionInset headerReference:(CGSize)headerReference footerReference:(CGSize)footerReference minimumInteritemSpacing:(CGFloat)minimumInteritemSpacing minimumLineSpacing:(CGFloat)minimumLineSpacing scrollDirection:(NSInteger)scrollDirection hidden:(BOOL)hidden tag:(int)tag userInteractionEnabled:(BOOL)userInteractionEnabled
  223. {
  224. UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
  225. //距离边界的上下左右间距
  226. [flowLayout setSectionInset:sectionInset];
  227. //头视图CGSize
  228. [flowLayout setHeaderReferenceSize:headerReference];
  229. //尾视图CGSize
  230. [flowLayout setFooterReferenceSize:footerReference];
  231. //竖向最大间距
  232. [flowLayout setMinimumInteritemSpacing:minimumInteritemSpacing];
  233. //横向最小间距
  234. [flowLayout setMinimumLineSpacing:minimumLineSpacing];
  235. //纵/横 排序
  236. [flowLayout setScrollDirection:scrollDirection==0?UICollectionViewScrollDirectionVertical:UICollectionViewScrollDirectionHorizontal];
  237. UICollectionView *newCollectionView = [[UICollectionView alloc]initWithFrame:frame collectionViewLayout:flowLayout];
  238. //RGBACOLOR(232, 232, 227, 1)
  239. [newCollectionView setBackgroundColor:backgroundColor];
  240. [newCollectionView setDelegate:delegate];
  241. [newCollectionView setDataSource:dataSource];
  242. [newCollectionView setScrollEnabled:scrollEnabled];
  243. //是否可以纵向滑动
  244. [newCollectionView setAlwaysBounceVertical:alwaysBounceVertical];
  245. //是否可以横向滑动
  246. [newCollectionView setAlwaysBounceHorizontal:alwaysBounceHorizontal];
  247. //是否展示横向浮动条
  248. [newCollectionView setShowsHorizontalScrollIndicator:showsHorizontalScrollIndicator];
  249. //是否展示竖向浮动条
  250. [newCollectionView setShowsVerticalScrollIndicator:showsVerticalScrollIndicator];
  251. [newCollectionView setHidden:hidden];
  252. [newCollectionView setTag:tag];
  253. [newCollectionView setUserInteractionEnabled:userInteractionEnabled];
  254. //注册Cell模板
  255. //[newCollectionView registerClass:[registerClass class] forCellWithReuseIdentifier:@"Cell"];
  256. //注册头视图模板
  257. // [newCollectionView registerClass:[headerView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];
  258. //注册尾视图模板
  259. // [newCollectionView registerClass:[footerView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView"];
  260. return newCollectionView;
  261. }
  262. #pragma mark - UISlider
  263. +(UISlider *)sliderInitWithFrame:(CGRect)frame minimumValue:(float)minimumValue maximumValue:(float)maximumValue value:(float)value thumbImage:(UIImage *)thumbImage minimumTrackImage:(UIImage *)minimumTrackImage maximumTrackImage:(UIImage *)maximumTrackImage minimumTrackImageResizableImageWithCapInsets:(UIEdgeInsets)minimumTrackImageResizableImageWithCapInsets resizingMode1:(NSInteger)resizingMode1 maximumTrackImageResizableImageWithCapInsets:(UIEdgeInsets)maximumTrackImageResizableImageWithCapInsets resizingMode2:(NSInteger)resizingMode2 target:(id)target action:(SEL)action hidden:(BOOL)hidden tag:(int)tag userInteractionEnabled:(BOOL)userInteractionEnabled
  264. {
  265. UISlider *slider = [[UISlider alloc]initWithFrame:frame];
  266. [slider setMinimumValue:minimumValue]; //最小值
  267. [slider setMaximumValue:maximumValue]; //最大值
  268. [slider setValue:value]; //默认值
  269. [slider setThumbImage:thumbImage forState:UIControlStateNormal];//滑动按钮
  270. minimumTrackImage = [minimumTrackImage resizableImageWithCapInsets:minimumTrackImageResizableImageWithCapInsets resizingMode:resizingMode1];
  271. [slider setMinimumTrackImage:minimumTrackImage forState:UIControlStateNormal];//滑动图层
  272. maximumTrackImage = [maximumTrackImage resizableImageWithCapInsets:maximumTrackImageResizableImageWithCapInsets resizingMode:resizingMode2];
  273. [slider setMaximumTrackImage:maximumTrackImage forState:UIControlStateNormal];//滑动底层
  274. [slider setHidden:hidden];
  275. [slider setTag:tag];
  276. [slider setUserInteractionEnabled:userInteractionEnabled];
  277. [slider addTarget:target action:action forControlEvents:UIControlEventValueChanged];
  278. return slider;
  279. }
  280. //UIProgressView
  281. #pragma mark - UIProgressView
  282. +(UIProgressView *)progressViewInitWithFrame:(CGRect)frame progressViewStyle:(NSInteger)progressViewStyle progress:(float)progress progressTintColor:(UIColor *)progressTintColor trackTintColor:(UIColor *)trackTintColor trackImage:(UIImage *)trackImage progressImage:(UIImage *)progressImage animated:(BOOL)animated tag:(int)tag hidden:(BOOL)hidden userInteractionEnabled:(BOOL)userInteractionEnabled
  283. {
  284. UIProgressView *progressView = [[UIProgressView alloc]init];
  285. //风格 感觉两种都差不多
  286. [progressView setProgressViewStyle:progressViewStyle];
  287. //设置的高度对进度条的高度没影响,整个高度=进度条的高度,进度条也是个圆角矩形
  288. [progressView setFrame:frame];
  289. //设置进度条上进度的颜色 默认是蓝色的
  290. [progressView setProgressTintColor:progressTintColor];
  291. //表示进度条未完成的,剩余的轨迹颜色,默认是灰色
  292. [progressView setTrackTintColor:trackTintColor];
  293. //设置进度条的背景图片
  294. [progressView setTrackImage:trackImage];
  295. //设置进度条上进度的背景图片
  296. [progressView setProgressImage:progressImage];
  297. //设置进度值并是否动画显示,这个相当于百分比,范围在0~1之间,不可以设置最大最小值
  298. [progressView setProgress:0.7 animated:animated];
  299. return progressView;
  300. }
  301. #pragma mark - 多颜色多字体的Label
  302. +(UILabel *)labelAttributedStringInitWithFrame:(CGRect)frame text1:(NSString *)text1 text2:(NSString *)text2 text3:(NSString *)text3 numberOfLines:(int)numberOfLines textAlignment:(NSTextAlignment)textAlignment textColor1:(UIColor *)textColor1 textColor2:(UIColor *)textColor2 textColor3:(UIColor *)textColor3 font:(UIFont *)font backgroundColor:(UIColor *)backgroundColor tag:(int)tag hidden:(BOOL)hidden userInteractionEnabled:(BOOL)userInteractionEnabled
  303. {
  304. NSString *str = [NSString stringWithFormat:@"%@%@%@",text1,text2,text3];
  305. NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@%@%@",text1,text2,text3]];
  306. NSRange range1=[[string string]rangeOfString:text1];
  307. [string addAttribute:NSForegroundColorAttributeName value:textColor1 range:range1];
  308. NSRange range2=[[string string]rangeOfString:text2];
  309. [string addAttribute:NSForegroundColorAttributeName value:textColor2 range:range2];
  310. NSRange range3=[[string string]rangeOfString:text3];
  311. [string addAttribute:NSForegroundColorAttributeName value:textColor3 range:range3];
  312. NSRange range4=[[string string]rangeOfString:str];
  313. [string addAttribute:NSFontAttributeName value:font range:range4];
  314. UILabel *label = [[UILabel alloc]initWithFrame:frame];
  315. [label setNumberOfLines:numberOfLines];
  316. [label setBackgroundColor:backgroundColor];
  317. [label setTextAlignment:textAlignment];
  318. [label setAttributedText:string];
  319. [label setTag:tag];
  320. [label setHidden:hidden];
  321. [label setUserInteractionEnabled:userInteractionEnabled];
  322. return label;
  323. }
  324. #pragma mark - JPushView 应用程序在前台时收到的推送消息展示UI
  325. +(UIView *)pushViewInitWithFrame:(CGRect)frame channel:(NSString *)channel pushText:(NSString *)pushText image:(UIImage *)image backgroundColor:(UIColor *)backgroundColor hidden:(BOOL)hidden tag:(int)tag userInteractionEnabled:(BOOL)userInteractionEnabled
  326. {
  327. UIView *view = [[UIView alloc]initWithFrame:frame];
  328. [view setBackgroundColor:backgroundColor];
  329. [view setHidden:hidden];
  330. [view setTag:tag];
  331. [view setUserInteractionEnabled:userInteractionEnabled];
  332. // [view setAlpha:.7];
  333. UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
  334. [imageView setFrame:CGRectMake(15, 10, 20, 20)];
  335. ViewRadius(imageView, 4);
  336. [view addSubview:imageView];
  337. UILabel *channelLabel = [[UILabel alloc]initWithFrame:CGRectMake(43, 8, 80, 20)];
  338. [channelLabel setBackgroundColor:[UIColor clearColor]];
  339. [channelLabel setText:channel];
  340. [channelLabel setTextColor:[UIColor whiteColor]];
  341. [channelLabel setTextAlignment:NSTextAlignmentLeft];
  342. [channelLabel setFont:[UIFont boldSystemFontOfSize:14]];
  343. [channelLabel setUserInteractionEnabled:YES];
  344. [view addSubview:channelLabel];
  345. UILabel *timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(123, 8, 30, 20)];
  346. [timeLabel setBackgroundColor:[UIColor clearColor]];
  347. [timeLabel setText:@"现在"];
  348. [timeLabel setTextColor:[UIColor whiteColor]];
  349. [timeLabel setTextAlignment:NSTextAlignmentLeft];
  350. [timeLabel setFont:[UIFont systemFontOfSize:12]];
  351. [timeLabel setUserInteractionEnabled:YES];
  352. [timeLabel setAlpha:.4];
  353. [view addSubview:timeLabel];
  354. UILabel *pushTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(43, 26, SCREEN_WIDTH-60, 35)];
  355. [pushTextLabel setBackgroundColor:[UIColor clearColor]];
  356. [pushTextLabel setText:pushText];
  357. [pushTextLabel setTextColor:[UIColor whiteColor]];
  358. [pushTextLabel setTextAlignment:NSTextAlignmentLeft];
  359. [pushTextLabel setFont:[UIFont systemFontOfSize:13]];
  360. [pushTextLabel setNumberOfLines:2];
  361. [pushTextLabel setUserInteractionEnabled:YES];
  362. [view addSubview:pushTextLabel];
  363. return view;
  364. }
  365. @end