#import "ViewController.h"
#import <CoreText/CoreText.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *btn=[[UIButton alloc]init];
NSMutableDictionary *btndict=[NSMutableDictionary dictionary];
btndict[NSFontAttributeName]=[UIFont systemFontOfSize:15];
[btn setTitleTextAttributes:btndict forState:UIControlStateNormal];
UITextField *textfield=[[UITextField alloc]init];
textfield.text=@"试试";
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:textfield.text];
[attrString addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(0, textfield.text.length)];
[textfield setAttributedText:attrString];
1.控件 UIView UILabel UITextField UITextView UIButton
2.字体、大小、单位、颜色
*/
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 300, 260)];
label.text = @"Label Text Content, This is a text label things attribute";
label.font = [UIFont systemFontOfSize:17];
label.textColor = [UIColor orangeColor];
label.shadowColor = [UIColor lightGrayColor];
label.shadowOffset = CGSizeMake(1,0);
label.textAlignment = NSTextAlignmentCenter;
label.lineBreakMode = NSLineBreakByTruncatingTail;
NSString *string = label.text;
const CGFloat fontSize = 16.0;
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string];
NSUInteger length = [string length];
UIFont *baseFont = [UIFont systemFontOfSize:fontSize];
[attrString addAttribute:NSFontAttributeName value:baseFont range:NSMakeRange(0, length)];
UIFont *boldFont = [UIFont boldSystemFontOfSize:fontSize];
[attrString addAttribute:NSFontAttributeName value:boldFont range:[string rangeOfString:@"Text"]];
UIFont *italicFont = GetVariationOfFontWithTrait(baseFont,
kCTFontTraitItalic);
[attrString addAttribute:NSFontAttributeName value:italicFont
range:[string rangeOfString:@"Label"]];
UIColor *color = [UIColor redColor];
[attrString addAttribute:NSForegroundColorAttributeName
value:color
range:[string rangeOfString:@"Content"]];
[attrString addAttribute:NSBackgroundColorAttributeName value:[UIColor blueColor] range:[string rangeOfString:@"ent"]];
[attrString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Verdana-BoldItalic" size:18] range:[string rangeOfString:@"Label"]];
label.numberOfLines = 2;
NSMutableParagraphStyle *
style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
style.lineSpacing = 10;
style.headIndent = 10;
style.tailIndent = -10;
style.lineHeightMultiple = 1.5;
style.alignment = NSTextAlignmentLeft;
style.firstLineHeadIndent = 20;
style.paragraphSpacing = 10;
style.paragraphSpacingBefore = 20;
[attrString addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, length)];
[attrString addAttribute:NSKernAttributeName value:@2 range:NSMakeRange(0, length)];
[attrString addAttribute:NSStrokeColorAttributeName value:[UIColor blueColor] range:[string rangeOfString:@"is"]];
[attrString addAttribute:NSStrokeWidthAttributeName value:@2 range:[string rangeOfString:@"is"]];
[attrString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:[string rangeOfString:@"text"]];
[attrString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleThick) range:[string rangeOfString:@"label"]];
[attrString addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:[string rangeOfString:@"things"]];
[attrString addAttribute:NSStrikethroughColorAttributeName value:[UIColor blueColor] range:[string rangeOfString:@"things"]];
label.attributedText = attrString;
label.highlightedTextColor = [UIColor redColor];
label.highlighted = NO;
label.enabled = YES;
label.userInteractionEnabled = YES;
label.baselineAdjustment = UIBaselineAdjustmentNone;
[self.view addSubview:label];
}
UIFont * GetVariationOfFontWithTrait(UIFont *baseFont,
CTFontSymbolicTraits trait) {
CGFloat fontSize = [baseFont pointSize];
CFStringRef
baseFontName = (__bridge CFStringRef)[baseFont fontName];
CTFontRef baseCTFont = CTFontCreateWithName(baseFontName,
fontSize, NULL);
CTFontRef ctFont =
CTFontCreateCopyWithSymbolicTraits(baseCTFont, 0, NULL,
trait, trait);
NSString *variantFontName =
CFBridgingRelease(CTFontCopyName(ctFont,
kCTFontPostScriptNameKey));
UIFont *variantFont = [UIFont fontWithName:variantFontName
size:fontSize];
CFRelease(ctFont);
CFRelease(baseCTFont);
return variantFont;
};
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)