? 在iOS開發過程中,經常會用到很多酷炫的文本,比如說文本中的某些文字要改變字號大小和文字顏色,某些文字要支持點擊功能等,這些如果我們用Label文本是比較難以實現的,所以我們采用富文本的形式來實現這些功能。代碼示例如下,效果圖往下翻:
/**
* #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;
}
?上面是對于文字顏色和文字字號的設置,下面我們來看下文本中支持點擊的富文本是神馬樣子滴?!
/**
* #pragma mark -- 可點擊的富文本 --
*/
+(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];
//獲取需點擊的文字
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;
}
?這個方法返回的富文本需配合使用UITextView使用,可點擊的文本需賦值給UITextView顯示,實現UItextView的這個代理:
- (BOOL) textView:(UITextView*)textView
shouldInteractWithURL:(NSURL*)URL
inRange:(NSRange)characterRange
?接下來我們再看下富文本中插入圖片,是怎么實現的,代碼來也。。。
+(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];
// 設置圖片大小
attch.bounds = imageBounds;//CGRectMake(0, -2, 22, 18);
// 創建帶有圖片的富文本
NSAttributedString *attStr = [NSAttributedString attributedStringWithAttachment:attch];
//圖片插入文本中的位置
[attri insertAttributedString:attStr atIndex:atIndex];
return attri;
}
?以上是用于UILabel和UITextView的文本顯示的,下面我們再來按下UIButton上使用富文本,構造出來的是什么效果呢,期待。。。往下看代碼實現:
/**
* #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;
}
?好了,大家如有好的想法和建議可留言相互交流學習,奉上以上的實現效果如下:
代碼測試.png