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;  斜体

  • 相关阅读:
    POJ 1161 Walls ( Floyd && 建图 )
    POJ 1252 Euro Efficiency ( 完全背包变形 && 物品重量为负 )
    POJ 3111 K Best ( 二分 )
    2017乌鲁木齐网络赛 J题 Our Journey of Dalian Ends ( 最小费用最大流 )
    POJ 2112 Optimal Milking ( 经典最大流 && Floyd && 二分 )
    POJ 3281 Dining ( 最大流 && 建图 )
    POJ 2391 Ombrophobic Bovines ( 经典最大流 && Floyd && 二分 && 拆点建图)
    冲刺第一周第一天
    学习进度条12/3到12/9
    四则运算2
  • 原文地址:https://www.cnblogs.com/jingdizhiwa/p/5613801.html
Copyright © 2011-2022 走看看