iOS 修改UILabel内容间距, 算合适的高度

UILabel 根据内容计算高度, 设置上下左右的间距

/// 1,给label设置要显示的字体,
self.label.text = @"你这个方法是在多个字体的时候又效果,如果说label很大,字体的个数很少,你这个设置的方法是在label的中心靠左边显示的,没有达到左上角显示字体的效果,我当时是用了label的自适应来写的,不知道你们还有有好的方法,希望可以讨论一下~,字体的个数很少,你这个设置的方法是在label的中心靠左边显示的,没有达到左上角显示字体的效果,我当时是用了label的自适应来写的,不知道你们还有有好的方法,希望可以讨论一下~";

/// 2, 给一个固定的宽度
self.label.frame = CGRectMake(10 , 10, kScreenW-40, 0);

/// 3, 通过宽度和字体算出高度
CGSize size = [self.label sizeThatFits:CGSizeMake(kScreenW-40, 0)];

/// 4, 设置label的frame
self.label.frame = CGRectMake(10, 10, size.width, size.height);

设置self.label.yf_contentInsets = UIEdgeInsetsMake(10, 10, 10, 10); 效果如下

Paste_Image.png

不设置 如下

Paste_Image.png

UILabel 添加分类后可以直接调用上述方法

.h 文件

#import <UIKit/UIKit.h>
@interface UILabel (YFAdd)
/**
  修改label内容距 `top` `left` `bottom` `right` 边距
 */
@property (nonatomic, assign) UIEdgeInsets yf_contentInsets;
@end

.m文件

#import "UILabel+YFAdd.h"
#import <objc/runtime.h>
/// 获取UIEdgeInsets在水平方向上的值
CG_INLINE CGFloat
UIEdgeInsetsGetHorizontalValue(UIEdgeInsets insets) {
    return insets.left + insets.right;
}

/// 获取UIEdgeInsets在垂直方向上的值
CG_INLINE CGFloat
UIEdgeInsetsGetVerticalValue(UIEdgeInsets insets) {
    return insets.top + insets.bottom;
}

CG_INLINE void
ReplaceMethod(Class _class, SEL _originSelector, SEL _newSelector) {
    Method oriMethod = class_getInstanceMethod(_class, _originSelector);
    Method newMethod = class_getInstanceMethod(_class, _newSelector);
    BOOL isAddedMethod = class_addMethod(_class, _originSelector, method_getImplementation(newMethod), method_getTypeEncoding(newMethod));
    if (isAddedMethod) {
        class_replaceMethod(_class, _newSelector, method_getImplementation(oriMethod), method_getTypeEncoding(oriMethod));
    } else {
        method_exchangeImplementations(oriMethod, newMethod);
    }
}

@implementation UILabel (YFAdd)
+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        ReplaceMethod([self class], @selector(drawTextInRect:), @selector(yf_drawTextInRect:));
        ReplaceMethod([self class], @selector(sizeThatFits:), @selector(yf_sizeThatFits:));
    });
}

- (void)yf_drawTextInRect:(CGRect)rect {
    UIEdgeInsets insets = self.yf_contentInsets;
    [self yf_drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}

- (CGSize)yf_sizeThatFits:(CGSize)size {
    UIEdgeInsets insets = self.yf_contentInsets;
    size = [self yf_sizeThatFits:CGSizeMake(size.width - UIEdgeInsetsGetHorizontalValue(insets), size.height-UIEdgeInsetsGetVerticalValue(insets))];
    size.width += UIEdgeInsetsGetHorizontalValue(insets);
    size.height += UIEdgeInsetsGetVerticalValue(insets);
    return size;
}

const void *kAssociatedYf_contentInsets;
- (void)setYf_contentInsets:(UIEdgeInsets)yf_contentInsets {
    objc_setAssociatedObject(self, &kAssociatedYf_contentInsets, [NSValue valueWithUIEdgeInsets:yf_contentInsets] , OBJC_ASSOCIATION_RETAIN);
}

- (UIEdgeInsets)yf_contentInsets {
    return [objc_getAssociatedObject(self, &kAssociatedYf_contentInsets) UIEdgeInsetsValue];
}


最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • iOS开发系列--网络开发 概览 大部分应用程序都或多或少会牵扯到网络开发,例如说新浪微博、微信等,这些应用本身可...
    lichengjin阅读 4,196评论 2赞 7
  • *7月8日上午 N:Block :跟一个函数块差不多,会对里面所有的内容的引用计数+1,想要解决就用__block...
    炙冰阅读 2,805评论 1赞 14
  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 9,435评论 3赞 38
  • 接《iOS知识点大总结一》 三十四、主线程操作UI(对UI进行更新只能在主线程进行) 解释:所谓的在主线程更新UI...
    RunningMan_Fly阅读 1,605评论 0赞 0
  • 文/冬月之恋 太阳一竿子高了,大地仿佛刚刚从几小时前的岑寂中苏醒过来,薄雾笼罩的天空一下子变得亮堂堂的,空气里闪着...
    冬月之恋阅读 820评论 12赞 18

友情链接更多精彩内容