Ios X和Y值未应用于标签
我正在尝试在drawRect中绘制标签。当设置数据时,我使用Ios X和Y值未应用于标签,ios,uilabel,drawrect,Ios,Uilabel,Drawrect,我正在尝试在drawRect中绘制标签。当设置数据时,我使用sizeToFit函数计算标签高度,然后用所需的X和Y值重新创建标签的框架 关于数据集: - (void)setNewsDetailDto:(NewsDetailDto *)newsDetailDto { _newsDetailDto = newsDetailDto; MediaDto *mediaDto = newsDetailDto.media[0]; NSLog(@"UIBPVCell - setNewsD
sizeToFit
函数计算标签高度,然后用所需的X和Y值重新创建标签的框架
关于数据集:
- (void)setNewsDetailDto:(NewsDetailDto *)newsDetailDto {
_newsDetailDto = newsDetailDto;
MediaDto *mediaDto = newsDetailDto.media[0];
NSLog(@"UIBPVCell - setNewsDetailDto - URL : %@", mediaDto.media);
NSURL *url = [NSURL URLWithString:mediaDto.media];
[[SDWebImageDownloader sharedDownloader]
downloadImageWithURL:url
options:SDWebImageDownloaderContinueInBackground | SDWebImageDownloaderHighPriority
progress:nil
completed:^(UIImage *_Nullable image, NSData *_Nullable data, NSError *_Nullable error, BOOL finished) {
CGSize scaleSize = CGSizeMake(image.size.width * screenWidth / image.size.height, screenWidth);
_newsImage = [self imageWithImage:image scaledToSize:scaleSize];
CGFloat x = (CGFloat) fabs(image.size.width - newsImageRect.size.width) / 2;
CGRect frame = CGRectMake(x, 0, screenWidth, screenWidth);
_newsImage = [self croppedImage:_newsImage inRect:frame];
labelTitle = [[UILabel alloc] init];
labelTitle.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:27.0f];
labelTitle.text = newsDetailDto.title;
labelTitle.textColor = [UIColor whiteColor];
labelTitle.numberOfLines = 0;
labelTitle.textAlignment = NSTextAlignmentLeft;
labelTitle.preferredMaxLayoutWidth = screenWidth - 36.0f;
[self setNeedsLayout];
[self setNeedsDisplay];
}];
}
- (void)layoutSubviews {
NSLog(@"UIBPVCell : layoutSubviews");
[super layoutSubviews];
[self calculateRects];
}
- (void)calculateRects {
screenWidth = [UIScreen mainScreen].bounds.size.width;
newsImageRect = CGRectMake(0, 0, screenWidth, screenWidth);
if (labelTitle) {
[labelTitle sizeToFit];
CGRect frame =
CGRectMake(
13,
floor(screenWidth - labelTitle.frame.size.height - 10),
screenWidth - 36,
ceil(labelTitle.frame.size.height));
labelTitle.frame = frame;
}
}
在drawRect中:
- (void)drawRect:(CGRect)rect {
NSLog(@"UIBPVCell : drawRect");
[super drawRect:rect];
[labelTitle drawTextInRect:rect];
}
- (void)drawRect:(CGRect)rect {
NSLog(@"UIBPVCell : drawRect");
[super drawRect:rect];
...
if (_newsDetailDto) {
[_newsDetailDto.title
drawInRect:titleRect
withAttributes:attributes];
}
}
但X和Y值不应用于标签:
它应该是用红色矩形指定的,我认为你必须重新布局视图,因为你正在更新标签的框架。 试试这个 更新框架后添加这些行。
我认为这应该行得通。使用
label.layer.cliptobunds=YES
。然后给标签一些背景色,并相应地调整标签。
希望这有帮助 我从这个问题中找到了答案: 要点是: 如果在视图中执行自定义绘图,则不能绘制UILabel 或者另一个UI元素,而是绘制文本本身 这是我的完整代码:
NSMutableParagraphStyle *paragraphStyle =
[[NSParagraphStyle defaultParagraphStyle] mutableCopy];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
paragraphStyle.alignment = NSTextAlignmentLeft;
UIFont *font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:27.0f];
attributes = @{
NSFontAttributeName: font,
NSForegroundColorAttributeName: [UIColor whiteColor],
NSParagraphStyleAttributeName: paragraphStyle };
titleSize = [_newsDetailDto.title
boundingRectWithSize:CGSizeMake(screenWidth - 36, 0)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes
context:nil].size;
titleSize = CGSizeMake(ceilf(titleSize.width), ceilf(titleSize.height));
[self setNeedsLayout];
[self layoutIfNeeded];
在drawRect中:
- (void)drawRect:(CGRect)rect {
NSLog(@"UIBPVCell : drawRect");
[super drawRect:rect];
[labelTitle drawTextInRect:rect];
}
- (void)drawRect:(CGRect)rect {
NSLog(@"UIBPVCell : drawRect");
[super drawRect:rect];
...
if (_newsDetailDto) {
[_newsDetailDto.title
drawInRect:titleRect
withAttributes:attributes];
}
}
尝试删除此行:[super-drawRect:rect];是否有理由在drawRect中执行此操作,而不仅仅是直接更新框架?我正在开发一个自定义视图,此视图将在集合视图单元中使用。因此,对于Performance,我决定直接在
drawRect
中绘制所有内容。我认为有一个拼写错误screenWidth-labelTitle.frame.size.height-10
。应为屏幕高度
?您是否尝试过设置一个大于13
和10
的数字?不,这不是故意的打字错误。我已经这样做了。我分享了显示setNeedsLayout
用法的更详细代码。setNeedsLayout之后也需要使用layoutifneed