【问题标题】:UILabel font takes time to updateUILabel 字体需要时间来更新
【发布时间】:2017-05-10 20:56:35
【问题描述】:
self.name.font = [UIFont fontWithName:@"OpenSans" size:17.0];
self.name.textColor = [UIColor redColor];

'name' 是 UILabel 类型。 我正在设置UILabelfontcolor。我一打开页面(我的视图控制器)就可以看到color 红色。但是,字体最初是常规的size 14,需要一些时间来更新。如果我在页面上停留一段时间,字体会自动更新为新的size 17

为什么 2 个属性的更新会有延迟?

【问题讨论】:

  • 您可能在情节提要文件中添加了静态文本。尝试删除它。
  • 这段代码你在哪里写的? (采用哪种方法)。以及您在 storyboard/xib 文件中为该标签设置了什么?
  • @SNarula 我从我的 .xib 文件中删除了与 UILabel 'name' 关联的静态文本。仍然需要时间来更新。
  • @Shreyank 在 .xib 文件中有一些随机文本和字体 openSans-bold 大小为 14。在初始化单元格时 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)索引路径;我正在设置标签的字体和颜色。
  • 尝试在 UITableViewCell 类中实现 layoutSubviews()。只需定义-(void)layoutSubviews() 并在其中设置标签字体大小。

标签: ios objective-c iphone fonts uilabel


【解决方案1】:

可能您正在更新主线程之外的 ui 元素。

所有 ui 元素都应该在主线程中更新。不这样做可能会导致用户界面不一致、更新延迟,有时还会崩溃。

如果您不在主线程中并且想要更新 ui 元素。检查iOS - Ensure execution on main thread 中的第二和第三个答案(不接受)以了解这一点。

【讨论】:

  • 把我的评论放在 dispatch_async(dispatch_get_main_queue(), ^{});并且工作完美!谢谢!
  • 其实换成 [[NSOperationQueue mainQueue] addOperationWithBlock:^ { //你的代码在这里 NSLog(@"Main Thread Code"); }];因为显然,堆栈溢出帖子中记录的先前方法存在一些问题
  • 不,即使您已经在主队列中,调用dispatch_async(dispatch_get_main_queue(), ^{}); 也没有任何问题。
  • 我没有添加在主线程中运行代码的方式,因为它已经在另一个链接中得到了很好的回答,所以只包含链接!
【解决方案2】:

检查字体文件是否存在,字体名称是否与@"OpenSans" 相同,大写。

可能是字体名称不匹配

您可以通过以下方式检查应用程序中的所有字体:

NSArray *familyNames = [[NSArray alloc] initWithArray:[UIFont familyNames]];
NSArray *fontNames;
NSInteger indFamily, indFont;
for (indFamily=0; indFamily<[familyNames count]; ++indFamily)
{
    NSLog(@"Family name: %@", [familyNames objectAtIndex:indFamily]);
    fontNames = [[NSArray alloc] initWithArray:
                 [UIFont fontNamesForFamilyName:
                  [familyNames objectAtIndex:indFamily]]];
    for (indFont=0; indFont<[fontNames count]; ++indFont)
    {
        NSLog(@"    Font name: %@", [fontNames objectAtIndex:indFont]);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-02
    • 2016-09-01
    • 1970-01-01
    • 2013-09-24
    相关资源
    最近更新 更多