標籤:style blog http color 使用 strong io 資料
簡易使用UILabel的富文本
使用效果:
源碼:
NSString+YX.h NSString+YX.m
//// NSString+YX.h// YXKit//// Copyright (c) 2014年 Y.X. All rights reserved.//#import <Foundation/Foundation.h>#import "ConfigAttributedString.h"@interface NSString (YX)// 建立富文本並配置富文本(NSArray中的資料必須是ConfigAttributedString對象合集)- (NSMutableAttributedString *)createAttributedStringAndConfig:(NSArray *)configs;// 用於搜尋一段字串在另外一段字串中的NSRange值- (NSRange)rangeFrom:(NSString *)string;// 本字串的range- (NSRange)range;@end
//// NSString+YX.m// YXKit//// Copyright (c) 2014年 Y.X. All rights reserved.//#import "NSString+YX.h"@implementation NSString (YX)- (NSMutableAttributedString *)createAttributedStringAndConfig:(NSArray *)configs{ NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self]; [configs enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { ConfigAttributedString *oneConfig = obj; [attributedString addAttribute:oneConfig.attribute value:oneConfig.value range:oneConfig.range]; }]; return attributedString;}- (NSRange)rangeFrom:(NSString *)string{ return [string rangeOfString:self];}- (NSRange)range{ return NSMakeRange(0, self.length);}@end
ConfigAttributedString.h ConfigAttributedString.m
//// ConfigAttributedString.h// NSMutableAttributedString//// Copyright (c) 2014年 Y.X. All rights reserved.//#import <Foundation/Foundation.h>@interface ConfigAttributedString : NSObject@property (nonatomic, strong, readonly) NSString *attribute; // 富文字屬性@property (nonatomic, strong, readonly) id value; // 富文本值@property (nonatomic, assign, readonly) NSRange range; // 富文本範圍值// 通用型配置+ (instancetype)attribute:(NSString *)attribute value:(id)value range:(NSRange)range;// 配置字型+ (instancetype)font:(UIFont *)font range:(NSRange)range;// 配置字型顏色+ (instancetype)foregroundColor:(UIColor *)color range:(NSRange)range;// 配置字型背景顏色+ (instancetype)backgroundColor:(UIColor *)color range:(NSRange)range;// 字型描邊顏色以及描邊寬度以及陰影(以下兩個方法可以一起使用)+ (instancetype)strokeColor:(UIColor *)color range:(NSRange)range;+ (instancetype)strokeWidth:(float)number range:(NSRange)range;+ (instancetype)shadow:(NSShadow *)shadow range:(NSRange)range;// 配置文字的中劃線+ (instancetype)strikethroughStyle:(NSInteger)number range:(NSRange)range;// 配置文字的底線+ (instancetype)underlineStyle:(NSInteger)number range:(NSRange)range;// 字間距+ (instancetype)kern:(float)number range:(NSRange)range;// 段落樣式(需要將UILabel中的numberOfLines設定成0才有用)+ (instancetype)paragraphStyle:(NSMutableParagraphStyle *)style range:(NSRange)range;@end
//// ConfigAttributedString.m// NSMutableAttributedString//// Copyright (c) 2014年 Y.X. All rights reserved.//#import "ConfigAttributedString.h"@interface ConfigAttributedString ()@property (nonatomic, strong) NSString *attribute;@property (nonatomic, strong) id value;@property (nonatomic, assign) NSRange range;@end@implementation ConfigAttributedString+ (instancetype)attribute:(NSString *)attribute value:(id)value range:(NSRange)range{ ConfigAttributedString *config = [self new]; config.attribute = attribute; config.value = value; config.range = range; return config;}+ (instancetype)font:(UIFont *)font range:(NSRange)range{ ConfigAttributedString *config = [self new]; config.attribute = NSFontAttributeName; config.value = font; config.range = range; return config;}+ (instancetype)foregroundColor:(UIColor *)color range:(NSRange)range{ ConfigAttributedString *config = [self new]; config.attribute = NSForegroundColorAttributeName; config.value = color; config.range = range; return config;}+ (instancetype)backgroundColor:(UIColor *)color range:(NSRange)range{ ConfigAttributedString *config = [self new]; config.attribute = NSBackgroundColorAttributeName; config.value = color; config.range = range; return config;}+ (instancetype)strikethroughStyle:(NSInteger)number range:(NSRange)range{ ConfigAttributedString *config = [self new]; config.attribute = NSStrikethroughStyleAttributeName; config.value = [NSNumber numberWithInteger:number]; config.range = range; return config;}+ (instancetype)paragraphStyle:(NSMutableParagraphStyle *)style range:(NSRange)range{ ConfigAttributedString *config = [self new]; config.attribute = NSParagraphStyleAttributeName; config.value = style; config.range = range; return config;}+ (instancetype)kern:(float)number range:(NSRange)range{ ConfigAttributedString *config = [self new]; config.attribute = NSKernAttributeName; config.value = [NSNumber numberWithFloat:number]; config.range = range; return config;}+ (instancetype)underlineStyle:(NSInteger)number range:(NSRange)range{ ConfigAttributedString *config = [self new]; config.attribute = NSUnderlineStyleAttributeName; config.value = [NSNumber numberWithInteger:number]; config.range = range; return config;}+ (instancetype)strokeColor:(UIColor *)color range:(NSRange)range{ ConfigAttributedString *config = [self new]; config.attribute = NSStrokeColorAttributeName; config.value = color; config.range = range; return config;}+ (instancetype)strokeWidth:(float)number range:(NSRange)range{ ConfigAttributedString *config = [self new]; config.attribute = NSStrokeWidthAttributeName; config.value = [NSNumber numberWithFloat:number]; config.range = range; return config;}+ (instancetype)shadow:(NSShadow *)shadow range:(NSRange)range{ ConfigAttributedString *config = [self new]; config.attribute = NSShadowAttributeName; config.value = shadow; config.range = range; return config;}@end
控制器源碼:
//// RootViewController.m// RichText//// Copyright (c) 2014年 Y.X. All rights reserved.//#import "RootViewController.h"#import "NSString+YX.h"@interface RootViewController ()@end@implementation RootViewController- (void)viewDidLoad{ [super viewDidLoad]; // 字串 NSString *str = @"未選擇的路-弗羅斯特\n黃色的樹林裡分出兩條路,\n可惜我不能同時去涉足,\n我在那路口久久佇立,\n我向著一條路極目望去,\n直到它消失在叢林深處。\n但我卻選了另外一條路,\n它荒草萋萋,十分幽寂,\n顯得更誘人、更美麗,\n雖然在這兩條小路上,\n都很少留下旅人的足跡,\n雖然那天清晨落葉滿地,\n兩條路都未經腳印汙染。\n啊,留下一條路等改日再見!\n但我知道路徑延綿無盡頭,\n恐怕我難以再回返。\n也許多少年後在某個地方,\n我將輕聲歎息把往事回顧,\n一片樹林裡分出兩條路,\n而我選了人跡更少的一條,\n從此決定了我一生的道路。"; // 設定組 NSArray *array = @[// 全域設定 [ConfigAttributedString font:[UIFont systemFontOfSize:10.f] range:[str range]], [ConfigAttributedString paragraphStyle:[self style] range:[str range]], // 局部設定 [ConfigAttributedString foregroundColor:[UIColor redColor] range:[@"未選擇的路" rangeFrom:str]], [ConfigAttributedString font:[UIFont systemFontOfSize:20.f] range:[@"未選擇的路" rangeFrom:str]]]; // 初始化富文本 UILabel *label = [[UILabel alloc] initWithFrame:self.view.bounds]; label.numberOfLines = 0; label.attributedText = [str createAttributedStringAndConfig:array]; [self.view addSubview:label];}// 段落樣式- (NSMutableParagraphStyle *)style{ NSMutableParagraphStyle *style = [NSMutableParagraphStyle new]; style.lineSpacing = 10.f; style.firstLineHeadIndent = 10.f; return style;}@end
設計原理:
我把配置抽象成了一個對象,一個對象可能對應著不同的配置,這樣寫起來就不會亂
使用的時候直接就在數組中配置
通過NSString的Category直接產生富文本賦值給attributedText即完成了富文本的操作
這才叫物件導向編程嘛:)