zoukankan      html  css  js  c++  java
  • UILabel

    // 设置label每一行文字的最大宽度
        // 为了保证计算出来的数值 跟 真正显示出来的效果 一致
        self.contentLabel.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width - 20;

    下面通过例子,给UILabel的文字后面跟上一个表情

    //创建NSTextAttachment
        NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
        attachment.image = [UIImage imageNamed:@"emoji"];
    
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"后面跟着图片"];
        [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 6)];
        [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:NSMakeRange(0, 6)];
        NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment];
        [attributedString insertAttributedString:attachmentString atIndex:6];
    
        //设置attributedText
        _label.attributedText = attributedString;

    效果如下:
    这里写图片描述

    上图的例子中,emoji图片偏大,位置也偏上,如何调整?参考Center NSTextAttachment image next to single line UILabel

    可以通过调整bounds来解决:

        NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
        attachment.image = [UIImage imageNamed:@"emoji"];
        attachment.bounds = CGRectMake(0, -5, 20, 20);

    调整之后的效果如下:
    这里写图片描述

    也可以继承NSTextAttachment,然后重写- (CGRect)attachmentBoundsForTextContainer:(NSTextContainer *)textContainer proposedLineFragment:(CGRect)lineFrag glyphPosition:(CGPoint)position characterIndex:(NSUInteger)charIndex方法:

    @interface MYNSAttachment : NSTextAttachment
    @end
    
    @implementation MYNSAttachment
    
    - (CGRect)attachmentBoundsForTextContainer:(NSTextContainer *)textContainer proposedLineFragment:(CGRect)lineFrag glyphPosition:(CGPoint)position characterIndex:(NSUInteger)charIndex
    {
        return CGRectMake( 0 , -5 , lineFrag.size.height , lineFrag.size.height);
    }
    @end

    UIFont

    UIFont代表字体,常见创建方法有以下几个:
    + (UIFont *)systemFontOfSize:(CGFloat)fontSize;   系统默认字体
    + (UIFont *)boldSystemFontOfSize:(CGFloat)fontSize;  粗体
    + (UIFont *)italicSystemFontOfSize:(CGFloat)fontSize;  斜体

  • 相关阅读:
    Oracle中查看所有表和字段以及表注释.字段注释
    利用Excel表格中的宏,轻松提取首字母
    IntelliJ IDEA 14 注册码
    oracle initialization or shutdown in progress解决方法
    IIS6.0 IIS7.5应用程序池自动停止的解决方法
    Yii1.1测试环境配置(一)
    流行界面库
    delphi image控件上画矩形的问题
    delphi中TQueue的使用问题
    ShellExecute函数的问题
  • 原文地址:https://www.cnblogs.com/jingdizhiwa/p/5613801.html
Copyright © 2011-2022 走看看