日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[控件] LabelView

發布時間:2023/12/31 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [控件] LabelView 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

LabelView

此LabelView是用來將Label顯示在固定的View上的,需要計算Label的高度與寬度.

源碼:

NSString+StringHeight.h 與?NSString+StringHeight.m

// // NSString+StringHeight.h // USA // // Created by YouXianMing on 14/12/10. // Copyright (c) 2014年 fuhuaqi. All rights reserved. // #import <Foundation/Foundation.h>@interface NSString (StringHeight)/*** 計算文本的高度** @param font 字體* @param width 固定的寬度** @return 高度*/ - (CGFloat)heightWithLabelFont:(UIFont *)font withLabelWidth:(CGFloat)width;/*** 計算文本的寬度** @param font 字體** @return 寬度*/ - (CGFloat)widthWithLabelFont:(UIFont *)font;@end // // NSString+StringHeight.m // USA // // Created by YouXianMing on 14/12/10. // Copyright (c) 2014年 fuhuaqi. All rights reserved. // #import "NSString+StringHeight.h"@implementation NSString (StringHeight)- (CGFloat)heightWithLabelFont:(UIFont *)font withLabelWidth:(CGFloat)width {CGFloat height = 0;if (self.length == 0) {height = 0;} else {// 字體NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:18.f]};if (font) {attribute = @{NSFontAttributeName: font};}// 尺寸CGSize retSize = [self boundingRectWithSize:CGSizeMake(width, MAXFLOAT)options:NSStringDrawingTruncatesLastVisibleLine |NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeadingattributes:attributecontext:nil].size;height = retSize.height;}return height; }- (CGFloat)widthWithLabelFont:(UIFont *)font {CGFloat retHeight = 0;if (self.length == 0) {retHeight = 0;} else {// 字體NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:18.f]};if (font) {attribute = @{NSFontAttributeName: font};}// 尺寸CGSize retSize = [self boundingRectWithSize:CGSizeMake(MAXFLOAT, 0)options:NSStringDrawingTruncatesLastVisibleLine |NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeadingattributes:attributecontext:nil].size;retHeight = retSize.width;}return retHeight; }@end

LabelView.h 與?LabelView.m

// // LabelView.h // YXMWeather // // Created by XianMingYou on 15/2/16. // Copyright (c) 2015年 XianMingYou. All rights reserved. // #import <UIKit/UIKit.h> #import "NSString+StringHeight.h"@interface LabelView : UIView/*** 文本*/ @property (nonatomic, strong) NSString *text;/*** 文本顏色*/ @property (nonatomic, strong) UIColor *textColor;/*** 文本字體*/ @property (nonatomic, strong) UIFont *font;/*** 背景色*/ @property (nonatomic, strong) UIColor *color;/*** 距離頂部的距離*/ @property (nonatomic) CGFloat gapFromTop;/*** 距離底部的距離*/ @property (nonatomic) CGFloat gapFromBottom;/*** 距離左側的距離*/ @property (nonatomic) CGFloat gapFromLeft;/*** 距離右側的距離*/ @property (nonatomic) CGFloat gapFromRight;/*** 創建出view*/ - (void)buildView;/*** 創建出默認配置的label** @param text 字符串* @param origin 起始位置** @return 實例對象*/ + (instancetype)createWithText:(NSString *)text atOrigin:(CGPoint)origin;@end // // LabelView.m // YXMWeather // // Created by XianMingYou on 15/2/16. // Copyright (c) 2015年 XianMingYou. All rights reserved. // #import "LabelView.h"@interface LabelView ()@property (nonatomic) CGFloat labelWidth; @property (nonatomic) CGFloat labelHeight;@property (nonatomic, strong) UILabel *label;@end@implementation LabelView- (void)buildView {// 設置labelself.label.text = self.text;self.label.font = self.font;self.label.textColor = self.textColor;// 獲取寬度self.labelWidth = [self.text widthWithLabelFont:self.font];self.labelHeight = [self.text heightWithLabelFont:self.font withLabelWidth:MAXFLOAT];self.label.width = self.labelWidth;self.label.height = self.labelHeight;// 計算間距self.label.x = self.gapFromLeft;self.label.y = self.gapFromTop;// 重新設置尺寸self.width = self.labelWidth + self.gapFromLeft + self.gapFromRight;self.height = self.labelHeight + self.gapFromTop + self.gapFromBottom;// 設置背景色if (self.color) {self.backgroundColor = self.color;} }@synthesize label = _label; - (UILabel *)label {if (_label == nil) {_label = [[UILabel alloc] initWithFrame:CGRectZero];_label.textAlignment = NSTextAlignmentCenter;[self addSubview:_label];}return _label; }+ (instancetype)createWithText:(NSString *)text atOrigin:(CGPoint)origin {LabelView *labelView = [[LabelView alloc] initWithFrame:CGRectMake(origin.x, origin.y, 0, 0)];labelView.color = [UIColor blackColor];labelView.text = text;labelView.textColor = [UIColor whiteColor];labelView.font = [UIFont fontWithName:LATO_BOLD size:8];labelView.gapFromLeft = 10.f;labelView.gapFromRight = 10.f;labelView.gapFromTop = 2.f;labelView.gapFromBottom = 2.f;[labelView buildView];return labelView; }@end

使用時候的源碼:

? ??LabelView *labelView = [LabelView createWithText:@"YouXianMing" atOrigin:CGPointMake(10, 90)];

? ? [self.view addSubview:labelView];

?

總結

以上是生活随笔為你收集整理的[控件] LabelView的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。