修改UILabel的UIEdgeInsets
来源:互联网 发布:java代码加密工具 编辑:程序博客网 时间:2024/06/04 18:17
1.首先:创建一个UILabel
的Category
;
.h
文件中
#import <UIKit/UIKit.h>@interface UILabel (XHZLableInsets)@property (nonatomic, assign) UIEdgeInsets xhzContentInsets;@end
一般情况下,category中是不能创建属性的,所以需要使用到runtime
.m
文件
#import "UILabel+XHZLableInsets.h"#import <objc/runtime.h>static void *kAssociatedXhzCntentInsets = &kAssociatedXhzCntentInsets;/// 获取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 originMethod = 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(originMethod), method_getTypeEncoding(originMethod)); }else{ method_exchangeImplementations(originMethod, newMethod); }}@implementation UILabel (XHZLableInsets)//重写load方法+ (void)load{ static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ ReplaceMethod([self class], @selector(drawTextInRect:), @selector(xhzDrawTextInRect:)); ReplaceMethod([self class], @selector(sizeThatFits:), @selector(xhzSzieThatFits:)); });}//替换原有的drawTextInRect方法- (void)xhzDrawTextInRect:(CGRect)rect{ UIEdgeInsets insets = self.xhzContentInsets; [self xhzDrawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];}//替换原有的sizeThatFits方法- (CGSize)xhzSzieThatFits:(CGSize)size{ if (CGSizeEqualToSize(size, CGSizeZero)) {// NSAssert(NO, @"label size can not be CGSizeZero"); } UIEdgeInsets insets = self.xhzContentInsets; size = [self xhzSzieThatFits:CGSizeMake(size.width - UIEdgeInsetsGetHorizontalValue(insets), size.height - UIEdgeInsetsGetVerticalValue(insets))]; size.width += UIEdgeInsetsGetHorizontalValue(insets); size.height += UIEdgeInsetsGetVerticalValue(insets); return size;}//set方法- (void)setXhzContentInsets:(UIEdgeInsets)xhzContentInsets{ objc_setAssociatedObject(self, &kAssociatedXhzCntentInsets, [NSValue valueWithUIEdgeInsets:xhzContentInsets], OBJC_ASSOCIATION_RETAIN);}//get方法- (UIEdgeInsets)xhzContentInsets{ return [objc_getAssociatedObject(self, &kAssociatedXhzCntentInsets) UIEdgeInsetsValue];}@end
调用(在调用的地方需要引用类目的头文件)
self.introlLabel.xhzContentInsets = UIEdgeInsetsMake(10, 10, 10, 10);
使用前:
使用后
阅读全文
0 0
- 修改UILabel的UIEdgeInsets
- UILabel 内容的UIEdgeInsets 和 UITextField的 leftView/rightView
- 设置 UILabel 和 UITextField 的 Padding 或 Insets (理解UIEdgeInsets)
- 设置 UILabel 和 UITextField 的 Padding 或 Insets (理解UIEdgeInsets)
- 设置 UILabel 和 UITextField 的 Padding 或 Insets (理解UIEdgeInsets)
- 设置 UILabel 和 UITextField 的 Padding 或 Insets (理解UIEdgeInsets)
- 设置 UILabel 和 UITextField 的 Padding 或 Insets (理解UIEdgeInsets)
- UIEdgeInsets 说明 UILabel 和 UITextField 的 Padding 或 Insets
- 关于iOS UILabel 设置 UIEdgeInsets
- 设置UIlabel偏移,UITextField偏移 UIEdgeInsets 说明 设置 UILabel 和 UITextField 的 Padding 或 Insets
- 利用UIEdgeInsets属性修改UIButton的图片,文字位置
- UIEdgeInsets 的作用
- 关于 UIEdgeInsets 的使用
- UIEdgeInsets
- UIEdgeInsets
- 关于iOS的UIEdgeInsets属性
- 关于iOS的UIEdgeInsets属性
- iOS修改UILabel的行间距
- SQL事务 基本认识
- java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlException
- Postman request test 实例
- spring boot 的pom.xml事例(包含了mybatis配置)
- 最大流的增广路模板
- 修改UILabel的UIEdgeInsets
- 1483: [HNOI2009]梦幻布丁 链表+启发式合并
- 实战rfc5766-turn-server和ice4j广域网通讯
- DevExpress GridView列标题自定义背景颜色
- 多级目录联动效果实现
- elasticeasrch安装java客启端
- python学习(七)----time模块相关函数
- IO基础之缓冲流的详解
- 一些运行时异常及其对应的例子