- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
/*
1.控件 UIView UILabel UITextField UITextView UIButton
2.字体、大小、单位、颜色
*/
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 300, 260)];
[self.window addSubview:label];
label.text = @"Label Text Content, This is a text label things attribute";/* 默认为空 */
label.numberOfLines = 0;
/* 文本的基本数据类型,属性字符串。以此为基础,如果这个设置了相应的属性,则会忽略上面设置的属性,默认为空 */
NSString *string = label.text;
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string];
NSUInteger length = [string length];
/* 段落样式 */
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
style.lineSpacing = 10;/* 设置行高 */
style.headIndent = 10;/* 头部缩进 */
style.lineHeightMultiple = 1.5;/* 行间距 */
style.alignment = NSTextAlignmentLeft;/* 对齐方式 */
style.firstLineHeadIndent = 20;/* 首行缩进 */
style.paragraphSpacing = 20;/* 段落后面的间距 */
style.paragraphSpacingBefore = 20;/* 段落前面的间距 */
style.minimumLineHeight = 10;/* 最小行距 */
style.maximumLineHeight = 30;/* 最大行距 */
[attrString addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, length)];
/* 设置字体 */
UIFont *baseFont = [UIFont systemFontOfSize:25];
[attrString addAttribute:NSFontAttributeName value:baseFont range:NSMakeRange(0, length)];/* 设置所有的字体 */
UIFont *boldFont = [UIFont boldSystemFontOfSize:40];
[attrString addAttribute:NSFontAttributeName value:boldFont range:[string rangeOfString:@"Text"]];/* 设置Text这四个字母的字体为粗体 */
/* 设置颜色 */
UIColor *color = [[UIColor alloc] initWithRed:127/255.0 green:1.0 blue:212/255.0 alpha:1.0];
[attrString addAttribute:NSForegroundColorAttributeName value:color range:[string rangeOfString:@"Label"]];
[attrString addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:[string rangeOfString:@"things"]];
/* 设置字体 */
[attrString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"AmericanTypewriter" size:18] range:[string rangeOfString:@"things"]];
[attrString addAttribute:NSObliquenessAttributeName value:@"3" range:[string rangeOfString:@"is"]];
/* 字符间距 */
[attrString addAttribute:NSKernAttributeName value:@8 range:[string rangeOfString:@"attribute"]];
/* 设置文字描边颜色, 宽度 */
[attrString addAttribute:NSStrokeColorAttributeName value:[UIColor colorWithRed:138/255.0 green:43/255.0 blue:226/255.0 alpha:1.0] range:[string rangeOfString:@"attribute"]];
[attrString addAttribute:NSStrokeWidthAttributeName value:@2 range:[string rangeOfString:@"attribute"]];
/* 下划线 */
[attrString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleDouble) range:[string rangeOfString:@"Content"]];
[attrString addAttribute:NSUnderlineColorAttributeName value:[UIColor greenColor] range:[string rangeOfString:@"Content"]];
[attrString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleThick) range:[string rangeOfString:@"This"]];/* 厚的 */
/* 删除线 */
[attrString addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlineStyleThick) range:[string rangeOfString:@"label"]];
[attrString addAttribute:NSStrikethroughColorAttributeName value:[UIColor redColor] range:[string rangeOfString:@"label"]];
label.attributedText = attrString;
label.highlightedTextColor = [UIColor redColor];
label.highlighted = NO;
label.enabled = YES;
[attrString release];
[label release];
[_window release];
return YES;
}
效果如⬇️