? 在iOS開發(fā)過(guò)程中八匠,經(jīng)常會(huì)用到很多酷炫的文本絮爷,比如說(shuō)文本中的某些文字要改變字號(hào)大小和文字顏色梨树,某些文字要支持點(diǎn)擊功能等坑夯,這些如果我們用Label文本是比較難以實(shí)現(xiàn)的,所以我們采用富文本的形式來(lái)實(shí)現(xiàn)這些功能抡四。代碼示例如下柜蜈,效果圖往下翻:
/**
* #pragma mark -- 富文本的封裝 --
*/
+ (NSMutableAttributedString*_Nullable)createMutableAttibutedStingWithPrefixStr:(NSString *_Nullable)prefixStr
SuffixStr:(NSString *_Nullable)SuffixStr
prefixStrColor:(UIColor *_Nullable)prefixStrColor
SuffixStrColor:(UIColor *_Nullable)SuffixStrColor
prefixStrFont:(CGFloat)prefixStrFont
SuffixStrFont:(CGFloat)SuffixStrFont
{
NSMutableAttributedString *mutableAttributedStr = [NSMutableAttributedString new];
// set family price
NSMutableAttributedString *mutabPrefixStr
= [[NSMutableAttributedString alloc]
initWithString:prefixStr
attributes:@{NSForegroundColorAttributeName : prefixStrColor,
NSFontAttributeName : [UIFont systemFontOfSize:prefixStrFont]}];
NSMutableAttributedString *mutabSuffixStr
= [[NSMutableAttributedString alloc]
initWithString:SuffixStr
attributes:@{NSForegroundColorAttributeName : SuffixStrColor,
NSFontAttributeName : [UIFont systemFontOfSize:SuffixStrFont]}];
[mutabPrefixStr appendAttributedString:mutabSuffixStr];
mutableAttributedStr = mutabPrefixStr;
return mutableAttributedStr;
}
?上面是對(duì)于文字顏色和文字字號(hào)的設(shè)置,下面我們來(lái)看下文本中支持點(diǎn)擊的富文本是神馬樣子滴指巡?淑履!
/**
* #pragma mark -- 可點(diǎn)擊的富文本 --
*/
+(NSMutableAttributedString *_Nullable)setAttributedStringWithRecommedAtt:(NSString *_Nullable)recommedAtt
recommedAttibutes:(nullable NSDictionary<NSString *, id> *)recommedAttibutes
contentAtt:(NSString *_Nullable)contentAtt
contentAttibutes:(nullable NSDictionary<NSString *, id> *)contentAttibutes
rangeOfString:(NSString *_Nullable)rangeOfString
linkAttributeValue:(id _Nullable )linkAttributeValue
{
NSMutableAttributedString *recommedAtti
= [[NSMutableAttributedString alloc]initWithString:recommedAtt
attributes:recommedAttibutes];
NSMutableAttributedString *contentAtti = [[NSMutableAttributedString alloc]initWithString:contentAtt
attributes:contentAttibutes];
//獲取需點(diǎn)擊的文字
if ([rangeOfString isEqualToString:@""] == NO ||
[rangeOfString isKindOfClass:[NSNull class]] == NO ||
rangeOfString != nil)
{
NSRange range = [[contentAtti string] rangeOfString:rangeOfString];
[contentAtti addAttribute:NSLinkAttributeName value:linkAttributeValue range:range];
}
[recommedAtti appendAttributedString:contentAtti];
return recommedAtti;
}
?這個(gè)方法返回的富文本需配合使用UITextView使用,可點(diǎn)擊的文本需賦值給UITextView顯示藻雪,實(shí)現(xiàn)UItextView的這個(gè)代理:
- (BOOL) textView:(UITextView*)textView
shouldInteractWithURL:(NSURL*)URL
inRange:(NSRange)characterRange
?接下來(lái)我們?cè)倏聪赂晃谋局胁迦雸D片秘噪,是怎么實(shí)現(xiàn)的,代碼來(lái)也勉耀。指煎。蹋偏。
+(NSMutableAttributedString *)lableInsertImageByString:(NSString *)string
needInsertImageNamed:(NSString *)imageNamed
imageBounds:(CGRect)imageBounds
insertAtIndex:(NSInteger)atIndex
{
//文本中放置圖片
NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:string];
// 添加圖片
NSTextAttachment *attch = [[NSTextAttachment alloc] init];
// 插入圖片
attch.image = [UIImage imageNamed:imageNamed];
// 設(shè)置圖片大小
attch.bounds = imageBounds;//CGRectMake(0, -2, 22, 18);
// 創(chuàng)建帶有圖片的富文本
NSAttributedString *attStr = [NSAttributedString attributedStringWithAttachment:attch];
//圖片插入文本中的位置
[attri insertAttributedString:attStr atIndex:atIndex];
return attri;
}
?以上是用于UILabel和UITextView的文本顯示的,下面我們?cè)賮?lái)按下UIButton上使用富文本至壤,構(gòu)造出來(lái)的是什么效果呢暖侨,期待。崇渗。字逗。往下看代碼實(shí)現(xiàn):
/**
* #pragma mark -- 自定義按鈕 --
*/
+ (void)customsButtonWithButton:(UIButton *)button
TopTitle:(NSString *_Nullable)topTitle
bottomTitle:(NSString *_Nullable)bottomTitle
topTitleFont:(CGFloat)topTitleFont
bottomTitleFont:(CGFloat)bottomTitleFont
topTitleColor:(UIColor *_Nullable)topTitleColor
bottomTitleColor:(UIColor *_Nullable)bottomTitleColor
titleAlignment:(NSTextAlignment)titleAlignment
imageName:(NSString *_Nullable)imageName
titleInsert:(UIEdgeInsets)titleInsert
imageInsert:(UIEdgeInsets)imageInsert
{
NSMutableAttributedString *topMutableStr
= [[NSMutableAttributedString alloc]
initWithString:[NSString stringWithFormat:@"%@\n",topTitle]
attributes:@{NSForegroundColorAttributeName : topTitleColor,
NSFontAttributeName : [UIFont systemFontOfSize:topTitleFont]}];
NSMutableAttributedString *bottomMutableStr
= [[NSMutableAttributedString alloc]
initWithString:bottomTitle
attributes:@{NSForegroundColorAttributeName : bottomTitleColor,
NSFontAttributeName : [UIFont systemFontOfSize:bottomTitleFont]}];
[topMutableStr appendAttributedString:bottomMutableStr];
[bottomTitle isEqualToString:@""]== NO ||
[bottomTitle isKindOfClass:[NSNull class]] == NO ||
bottomTitle != nil ? [button setAttributedTitle:topMutableStr forState:UIControlStateNormal]
: [button setTitle:topTitle forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
button.titleEdgeInsets = titleInsert;
button.imageEdgeInsets = imageInsert;
button.titleLabel.numberOfLines = 0;
button.titleLabel.textAlignment = titleAlignment;
button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
}
?好了,大家如有好的想法和建議可留言相互交流學(xué)習(xí),奉上以上的實(shí)現(xiàn)效果如下:
代碼測(cè)試.png