iOS UIKit(一)

一、简介

UIKit 框架提供构建和管理 iOS 和 TVOS 应用程序的用户页面。响应用户互动和系统事件,访问各种设备功能,启用辅助功能,以及使用动画,文字和图片。在watchOS应用程序中,启用辅助功能并使用字体和图像。

1.DocumentManager

2.NSAttributedString

(1)属性简介

  • NSFontAttributeName(字体)
    /*
      该属性所对应的值是一个 UIFont 对象。该属性用于改变一段文本的字体。如果不指定该属性,则默认为12-point Helvetica(Neue)。
    */
    NSString *text = @"15号Courier-BoldOblique字体";
    // 1.创建NSMutableAttributedString实例
    NSMutableAttributedString *fontAttributeNameStr = [[NSMutableAttributedString alloc]initWithString:text];
    // 2.添加属性
    [fontAttributeNameStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Courier-BoldOblique" size:15] range:NSMakeRange(0, text.length)];
    //3.赋值label
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(50, 200, 300, 50)];
    label.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:label];
    label.attributedText = fontAttributeNameStr;
  • NSParagraphStyleAttributeName(段落)
    /*
      该属性所对应的值是一个 NSParagraphStyle 对象。该属性在一段文本上应用多个属性(行间距、缩进等)。如果不指定该属性,则默认为 defaultParagraphStyle 方法返回的默认段落属性。
    */
    //创建NSMutableParagraphStyle实例
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    //字间距5
    paragraphStyle.lineSpacing = 5;
    //行间距是20
    paragraphStyle.paragraphSpacing = 20;
    //对齐方式为居中对齐
    paragraphStyle.alignment = NSTextAlignmentCenter;
    [attributeStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, text.length)];
  • NSForegroundColorAttributeName(字体颜色)
    /*
     该属性所对应的值是一个 UIColor 对象。该属性用于指定一段文本的字体颜色。如果不指定该属性,则默认为黑色。
    */
    [attributeStr addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, text.length)];
  • NSBackgroundColorAttributeName(字体背景色)
    /*
     该属性所对应的值是一个 UIColor 对象。该属性用于指定一段文本的背景颜色。如果不指定该属性,则默认无背景色。
    */
    [attributeStr addAttribute:NSBackgroundColorAttributeName value:[UIColor purpleColor] range:NSMakeRange(0, text.length)];
  • NSLigatureAttributeName(连字符)
    /*
     该属性所对应的值是一个 NSNumber 对象(整数)。连体字符是指某些连在一起的字符,它们采用单个的图元符号。
       0 表示没有连体字符
       1 表示使用默认的连体字符
       2 表示使用所有连体符号
     默认值为 1(注意,iOS 不支持值为 2)
     */
    [attributeStr addAttribute:NSLigatureAttributeName value:[NSNumber numberWithInt:1] range:NSMakeRange(0, text.length)];
  • NSKernAttributeName(字间距)
    /*
     NSKernAttributeName 设定字符间距,取值为 NSNumber 对象(整数),正值间距加宽,负值间距变窄。
     */
    [attributeStr addAttribute:NSKernAttributeName value:@20 range:NSMakeRange(0, 18)];
  • NSStrikethroughStyleAttributeName(删除线)
    /*
     NSStrikethroughStyleAttributeName(删除线)NSStrikethroughStyleAttributeName 设置删除线,取值为 NSNumber 对象(整数),枚举常量 NSUnderlineStyle中的值:
     NSUnderlineStyleNone     默认值
     NSUnderlineStyleNone     不设置删除线
     NSUnderlineStyleSingle   设置删除线为细单实线
     NSUnderlineStyleThick    设置删除线为粗单实线
     NSUnderlineStyleDouble   设置删除线为细双实线
     */
    [attributeStr addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlineStyleThick) range:NSMakeRange(0, text.length)];
  • NSStrikethroughColorAttributeName(删除线颜色)
    /*
      NSUnderlineColorAttributeName 设置下划线颜色,取值为 UIColor 对象,默认值为黑色
     */
    [attributeStr addAttribute:NSStrikethroughColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, text.length)];
  • NSUnderlineStyleAttributeName(下划线)
    /*
     NSUnderlineStyleAttributeName(下划线)取值为 NSNumber 对象(整数),枚举常量 NSUnderlineStyle中的值:
     NSUnderlineStyleNone     默认值
     NSUnderlineStyleNone     不设置删除线
     NSUnderlineStyleSingle   设置删除线为细单实线
     NSUnderlineStyleThick    设置删除线为粗单实线
     NSUnderlineStyleDouble   设置删除线为细双实线
     */
    [attributeStr addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleThick) range:NSMakeRange(0, text.length)];
  • NSUnderlineColorAttributeName(下划线颜色)
    /*
     NSUnderlineColorAttributeName 设置下划线颜色,取值为 UIColor 对象,默认值为黑色
     */
    [attributeStr addAttribute:NSUnderlineColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, text.length)];
  • NSStrokeColorAttributeName(描边颜色)
  • NSStrokeWidthAttributeName(描边宽度)
    /*
     描边颜色要搭配非0的描边宽度才会生效,如果只设置了描边颜色,描边宽度为0,则没有描边效果
     描边宽度是正数,会对文字进行描边,但文字中心不填充( 一种经典的空心文本样式是在该值为3.0)
     描边宽度是负数,会对文字进行描边,而且会同时对文字中心进行填充(填充的颜色为文字本来的字体颜色)
     */
    //设置描边颜色
    [attributeStr addAttribute:NSStrokeColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, text.length)];
    //空心
    [attributeStr addAttribute:NSStrokeWidthAttributeName value:@(3) range:NSMakeRange(0, text.length)];
    //实心
    [attributeStr addAttribute:NSStrokeWidthAttributeName value:@(-3) range:NSMakeRange(0, text.length)];
  • NSShadowAttributeName(阴影)
    // 创建NSShadow实例
    NSShadow *shadow = [[NSShadow alloc] init];
    shadow.shadowColor = [UIColor purpleColor];
    shadow.shadowBlurRadius = 3.0;
    shadow.shadowOffset = CGSizeMake(0, 0.8);
    // 添加属性
    [attributeStr addAttribute:NSShadowAttributeName value:shadow range:NSMakeRange(0, text.length)];
  • NSTextEffectAttributeName(文字效果)
    /*
     设置文本特殊效果,目前只有图版印刷效果可用,NSString类型
     */
    [attributeStr addAttribute:NSTextEffectAttributeName value:NSTextEffectLetterpressStyle range:NSMakeRange(10, text.length - 10)];
  • NSLinkAttributeName(链接)
- (void)viewDidLoad {
    [super viewDidLoad];
    /*
     UILabel无法使用该属性, 但UITextView 控件可以使用,所以下面关于 NSLinkAttributeName 属性的代码也是使用 UITextView 来测试的。
     UITextView的editable设置为NO,编辑状态不可以跳转
     UITextView设置delegate
     */
    NSString *text = @"跳转到百度!";
    NSMutableAttributedString * attributeStr = [[NSMutableAttributedString alloc] initWithString:text];
    NSURL *url = [NSURL URLWithString:@"https://www.baidu.com"];
    [attributeStr addAttribute:NSLinkAttributeName value:url range:NSMakeRange(0, text.length)];
    //使用UITextView
    UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(50, 200, 300, 200)];
    textView.backgroundColor = [UIColor lightGrayColor];
    textView.delegate = self;
    textView.editable = NO;
    [self.view addSubview:textView];
    textView.attributedText = attributeStr;
    
}
// 注意:跳转链接要实现UITextView的这个委托方法
-(BOOL)textView:(UITextView *)textView shouldInteractWithURL:(nonnull NSURL *)URL inRange:(NSRange)characterRange forInteractionType:(UITextItemInteraction)interaction{
    return YES;
}
  • NSBaselineOffsetAttributeName(基础偏移量)
/*
     正值向上偏移,负值向下偏移,默认0(不偏移)
     */
    //向上偏移
    [attributeStr addAttribute:NSBaselineOffsetAttributeName value:@(10) range:NSMakeRange(0, 7)];
    //向下偏移
    [attributeStr addAttribute:NSBaselineOffsetAttributeName value:@(-10) range:NSMakeRange(13, text.length - 13)];
  • NSObliquenessAttributeName(字体倾斜)
    /*
     正值向右倾斜,负值向左倾斜, 默认0(不倾斜)
     */
    [attributeStr addAttribute:NSObliquenessAttributeName value:@(1) range:NSMakeRange(0, 7)];
    [attributeStr addAttribute:NSObliquenessAttributeName value:@(-1) range:NSMakeRange(13, text.length - 13)];
  • NSExpansionAttributeName(文本扁平化,横向拉伸)
    /*
     正值横向拉伸,负值横向压缩,默认0(不拉伸)
     */
    [attributeStr addAttribute:NSExpansionAttributeName value:@(0.8) range:NSMakeRange(0, 7)];
    [attributeStr addAttribute:NSExpansionAttributeName value:@(-0.8) range:NSMakeRange(13, text.length - 13)];
3.NSDataAsset

用于访问用特定格式存储在asset catalog里面的一个对象。特别注意一下,你在asset里建的文件一定是Data类型的,不然读不出来。

    NSDataAsset *dataAsset=[[NSDataAsset alloc]initWithName:@"chat_file_p"];
3EF69CB3-6C62-43FF-B8F3-69A063D429A0.png
4.NSFileProviderExtension

5.NSIndexPath+UIKitAdditions

Foundation.framework框架中NSIndexPath的分类。

    //NSIndexPath的创建方法
    NSIndexPath *indePath = [NSIndexPath indexPathForRow:1 inSection:0];
6.NSItemProvider+UIKitAdditions

Foundation.framework框架中NSItemProvider的分类。

7.NSLayoutAnchor、NSLayoutConstraint

NSLayoutAnchor其实是一个工厂类,用NSLayoutAnchor来创建NSLayoutConstraint对象,使用这些对象实现自动布局。一般不会直接创建NSLayoutConstraint对象,而是用UIView(NSView)、其子类或者UILayoutGuide的某个anchor属性。这些属性对于Auto Layout中主要的NSLayoutAttribute值,所以也可以用NSLayoutAnchor子类创建这些NSLayoutAttribute值。
NSLayoutAnchor有三个子类,对应三种锚点类型:

  • NSLayoutDimension(尺寸):宽widthAnchor、高heightAnchor
  • NSLayoutXAxisAnchor(水平方向x轴):左leftAnchor、右rightAnchor、前leadingAnchor、后trailingAnchor、水平中心centerXAnchor
  • NSLayoutYAxisAnchor(垂直方向y轴):上topAnchor、下bottomAnchor、垂直中心centerYAnchor、上基准线firstBaselineAnchor、下基准线lastBaselineAnchor
    需要注意:leaddingAnchor和leftAnchor,rightAnchor和trailingAnchor不能混用。
    UILabel  *titleLabel = [[UILabel alloc] init];
    titleLabel.numberOfLines = 0;
    titleLabel.backgroundColor = [UIColor redColor];
    [self.view addSubview:titleLabel];
    //关闭frame确定位置,开启autolayout布局
    titleLabel.translatesAutoresizingMaskIntoConstraints = false;
    [titleLabel.topAnchor constraintEqualToAnchor:self.view.topAnchor constant:100].active = true;
    [titleLabel.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor constant:1];
    [titleLabel.leftAnchor constraintEqualToAnchor:self.view.leftAnchor constant:12].active = true;
    [titleLabel.rightAnchor constraintEqualToAnchor:self.view.rightAnchor constant:-12].active = true;
    [titleLabel.heightAnchor constraintEqualToConstant:99].active = true;
8.NSLayoutManager、NSTextContainer、NSTextStorage

20130817133242937.png
  • NSTextContainer定义了文本可以排版的区域。默认情况下是矩形区域,如果是其他形状的区域,需要通过子类化NSTextContainer来创建。
  • NSLayoutManager该类负责对文字进行编辑排版处理,将存储在NSTextStorage中的数据转换为可以在视图控件中显示的文本内容,并把字符编码映射到对应的字形上,然后将字形排版到NSTextContainer定义的区域中。
  • NSTextStorage主要用来存储文本的字符和相关属性,是NSMutableAttributedString的子类。此外,当NSTextStorage中的字符或属性发生改变时,会通知NSLayoutManager,进而做到文本内容的显示更新。
-(void)viewDidLoad{
    [super viewDidLoad];
    // 数据源
    NSString *string = @"213dfsnf nnsdl;msc,mdsl,vbmnfgkdls;,dmfnghjfewp2;34htigopfd;sfvahjfsdnghdsnfghjskghojfdkgjhdksgdkskgnldkkgnlmd,ngkmd,lsmewn3b4hi5otyph;bv,mfdnhwio34p5t;g,mnvjde4tly;";
    
    // 文本容器
    NSTextStorage *storage = [[NSTextStorage alloc] initWithString:string];
    
    // 文本容器的布局经管器
    NSLayoutManager *layoutManager = [NSLayoutManager new];
    [storage addLayoutManager:layoutManager];
    
    // 分段显示文本容器中的内容
    CGSize size = CGSizeMake(100,50);
    NSTextContainer *textContainer1 = [[NSTextContainer alloc] initWithSize:size];
    [layoutManager addTextContainer:textContainer1];
    NSTextContainer *textContainer2 = [[NSTextContainer alloc] initWithSize:size];
    [layoutManager addTextContainer:textContainer2];
    NSTextContainer *textContainer3 = [[NSTextContainer alloc] initWithSize:size];
    [layoutManager addTextContainer:textContainer3];
    NSTextContainer *textContainer4 = [[NSTextContainer alloc] initWithSize:size];
    [layoutManager addTextContainer:textContainer4];
    
    // 给TextView添加带有内容和布局的容器
    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake (50, 100, size.width,size.height) textContainer:textContainer1];//textContainer2 textContainer3 textContainer4
    textView.layer.borderWidth = 1;
    textView.scrollEnabled = NO;
    textView.editable = NO;
    [self.view addSubview:textView];
    // 验证
    if (textView.textStorage == storage){
        NSLog(@"textView.textStorage == storage");
    }
    
    if (textView.layoutManager == layoutManager){
        NSLog(@"textView.layoutManager == layoutManager");
    }
    NSLog(@"策画的页码数:%f",[textView sizeThatFits:CGSizeMake(300,FLT_MAX)].height / 50);
}
9.NSParagraphStyle

段落设置

    NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
    // 行间距
    paragraphStyle.lineSpacing = 20;
    // 行高倍数(1.5倍行高)
    paragraphStyle.lineHeightMultiple = 1.5;
    //首行缩进
    paragraphStyle.firstLineHeadIndent = 30.0f;
    //最低行高
    paragraphStyle.minimumLineHeight = 10;
    //最大行高(会影响字体)
    paragraphStyle.maximumLineHeight = 20;
    // 对齐方式
    paragraphStyle.alignment = NSTextAlignmentLeft;
    // 默认Tab 宽度
    paragraphStyle.defaultTabInterval = 144;
    // 起始 x位置
    paragraphStyle.headIndent = 20;
    // 结束 x位置(不是右边间距,与inset 不一样)
    paragraphStyle.tailIndent = 320;
    // 段落间距
    paragraphStyle.paragraphSpacing = 44.;
    // 段落头部空白(实测与上边的没差啊?)
    paragraphStyle.paragraphSpacingBefore = 44.0;
    // 分割模式
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    // 段落方向
    paragraphStyle.baseWritingDirection = NSWritingDirectionRightToLeft;
10.NSShadow

阴影效果。

    NSShadow *shadow = [[NSShadow alloc]init];
    //阴影的偏移量
    shadow.shadowOffset = CGSizeMake(10, 10);
    //阴影颜
    shadow.shadowColor = [UIColor blueColor];
    //阴影半径
    shadow.shadowBlurRadius = 10;
11.NSStringDrawing

NSString类的扩展

12.NSText

NSText为用户界面对象绘制文本,提供文本编辑功能,并控制文本属性,如类型大小、字体和颜色。
NSText初始化创建具体子类的实例,例如nstextview(通常称为文本对象)。

13. NSTextAttachment

图文混排

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 50)];
    label.backgroundColor = [UIColor blueColor];
    [self.view addSubview:label];
    
    NSMutableAttributedString *textAttrStr = [[NSMutableAttributedString alloc] init];
    //添加图片
    NSTextAttachment *attach = [[NSTextAttachment alloc] init];
    attach.image = [UIImage imageNamed:@"qingchu"];
    attach.bounds = CGRectMake(0, 0 , 30, label.font.pointSize);
    NSAttributedString *imgStr = [NSAttributedString attributedStringWithAttachment:attach];
    [textAttrStr appendAttributedString:imgStr];
    [textAttrStr appendAttributedString:[[NSAttributedString alloc] initWithString:@"测试测试测试"]];
    label.attributedText = textAttrStr;
}

你可能感兴趣的:(iOS UIKit(一))