如何在 iPhone UILabel 中设置字距调整

2023-12-28

我正在开发一个 iPhone 应用程序,我想在 UILabel 中设置字距调整。我写的代码(可能围绕kCTKernAttributeName)似乎有错误。我该如何解决这个问题?

NSMutableAttributedString *attStr;   
NSString *str = @"aaaaaaa";    
CFStringRef kern = kCTKernAttributeName;        
NSNumber *num = [NSNumber numberWithFloat: 2.0f];    
NSDictionary *attributesDict = [NSDictionary dictionaryWithObject:num 
forKey:(NSString*)kern];        
[attStr initWithString:str attributes:attributesDict];      
CGRect frame1 = CGRectMake(0, 0, 100, 40);    
UILabel *label1 = [[UILabel alloc] initWithFrame:frame1];    
label1.text = attStr    
[self.view addSubview:label1];

老问题,但你现在就可以(很容易)做到。

NSMutableAttributedString *attributedString;
attributedString = [[NSMutableAttributedString alloc] initWithString:@"Please get wider"];
[attributedString addAttribute:NSKernAttributeName value:@5 range:NSMakeRange(10, 5)];
[self.label setAttributedText:attributedString];

2013 年 11 月,为了扩展这个伟大的答案,这里有一些完全典型的代码。通常你也会设置字体。请注意评论中使用普通旧 .text 的老式方式。希望它可以帮助某人

NSString *yourText = @"whatever";

UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)];

// simple approach with no tracking...
// label.text = yourText;
// [label setFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:24]];

NSMutableAttributedString *attributedString;

attributedString = [[NSMutableAttributedString alloc] initWithString:yourText];

[attributedString addAttribute:NSKernAttributeName
                         value:[NSNumber numberWithFloat:2.0]
                         range:NSMakeRange(0, [yourText length])];

[attributedString addAttribute:NSFontAttributeName
                         value:[UIFont fontWithName:@"HelveticaNeue-Light" size:24]
                         range:NSMakeRange(0, [yourText length])];

label.attributedText = attributedString;

label.textColor = [UIColor blackColor];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = NSTextAlignmentCenter;

[label sizeToFit];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 iPhone UILabel 中设置字距调整 的相关文章

随机推荐

  • 使用 C# 和 UI Automation 获取未知控件类型的内容

    下图中有一个区域 其具有未知 自定义 类别 那不是网格或表格 我需要能够 选择该区域中的行 从每个单元格中获取一个值 问题是因为这不是一个常见的类型元素 我不知道如何用谷歌搜索这个问题或自己解决它 到目前为止 代码如下 Process pr
  • 交换结构体数组中的元素

    假设我有这个结构 struct MyStruct int iID int iMyNumber 然后我定义一个 MyStructs 数组 struct MyStruct msTest 3 我正在通过查看 ID 对类似于此的结构进行排序操作 现
  • 在 Nuxt.js (SPA) 中使用“

    我使用 nuxt js express 启动该项目 我们为 nuxt js 中的每个组件 vue 开发了样式 因此 在路由时 属性会叠加在同一个类名 样式 上 从而导致页面无法正常显示 1 样式范围 的正确用法是什么 2 或者路由过程应该是
  • 如何在 Notepad++ 中模仿 Visual Studio 的 CTRL-X、CTRL-V 功能?

    I m using Notepad for some projects and miss Visual Studio s Ctrl X Ctrl C functionality that cuts or copies the entire
  • 从另一个表单显示一个表单

    当我想通过单击另一个表单中的按钮来显示表单 C 时 我通常从要显示的表单创建一个对象并使用 show 方法 Form2 f2 new Form2 f2 Show 或者我与 所有者 一起工作 Form2 tempForm new Form2
  • 如何使用 WhenCustomCommandReceived 设置 Topshelf?

    我正在使用 Topshelf 创建一个 Windows 服务 ServiceClass 并且我正在考虑使用 WhenCustomCommandReceived 发送自定义命令 HostFactory Run x gt x EnablePau
  • 在使用 reticulate 创建的 virtualenv 中安装特定版本的 python 包

    使用时reticulate为了在 R 中使用 Python 我们可以通过命令创建一个 virtualenvreticulate virtualenv create指定 env 名称和 python bin 的路径 我们还可以将包添加到之前创
  • 将日期发布到 Web API 的正确方法?

    我正在从 Web API 读取数据并填充表单 当我将其提交回 Web API 时 出现以下错误 Message 发生错误 ExceptionMessage 属性 类型 MvcApplication1 Models ProductSale 上
  • 使此 WCF 客户端代码在 Mac 上的 Mono 和 MonoTouch 上运行?

    我试图让以下代码在 Mono 控制台应用程序中工作 并作为 MonoTouch 应用程序的最终目标 该代码在使用 Windows 控制台应用程序的 VS2008 下运行良好 当在 Mac 上将其作为 Mono 控制台应用程序运行时 我得到
  • 为 CoffeeScript 声明不同的编译路径

    我有一个 Scalatra 应用程序可以编译 CoffeeScript 使用https github com softprops coffeescripted sbt https github com softprops coffeescr
  • Spring MVC 打开PDF作为视图

    哪个 View 类适合渲染现有 PDF 抽象视图 我通过网络服务获取 PDF 所以我不打算子类 Abstract PdfView 来渲染 PDF 我想继续使用返回 ModelAndView 的 Spring 控制器类 这意味着编写我自己的
  • 进程在读取时挂起

    以下代码通过管道从其他进程读取消息 所有进程都正确打印出所有消息 但它们将永远不会继续经过 while 循环 尝试在 Eclipse 中进行调试 在阅读完所有消息后 它将停止在 while 循环处 索引是分配给每个进程的编号 第一个进程的索
  • 如何使用 PHP 生成二维码?

    我正在尝试将一些数据转换为网页上的二维码 为此 我使用 php qrcode Sourceforge 图书馆主页 http phpqrcode sourceforge net http phpqrcode sourceforge net 我
  • 将图片插入Excel单元格[关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我想生成一份带有图片的报告 但我无法将图片放入单个单元格中 我可以让图片 浮动 在我的工作表周围 但我需要将它们放入单元格中 我怎样才能做
  • getLocationOnScreen() 与 getLocationInWindow()

    在这两种方法的上下文中 屏幕和视图有什么区别 我有一个按钮 我想获取其中心的 x 坐标 我想这就足够了 public int getButtonXPosition return button getLeft button getRight
  • 调用 ReadAsStreamAsync 时何时或是否要 Dispose HttpResponseMessage?

    我正在使用System Net Http HttpClient http msdn microsoft com en us library system net http httpclient v vs 118 aspx进行一些客户端 HT
  • 返回自定义 C++ 类型的 Q_INVOKABLE 方法

    我有一个 C 方法 设为 Q INVOKABLE 我可以从 QML 调用此方法 它在返回基本类型 如 QString 时起作用 但我不能使用自定义类型 我该怎么做 我应该返回 QVariant 吗 理想情况下 如果可能的话 我想返回一个指向
  • 将枚举转换为另一种类型的枚举

    我有一个枚举 例如 Gender Male 0 Female 1 我有另一个来自服务的枚举 该服务有自己的性别枚举 Male 0 Female 1 Unknown 2 我的问题是我怎样才能写一些快速而漂亮的东西来从他们的枚举转换为我的枚举
  • 了解 CLR 2.0 内存模型

    乔 达菲 给予描述 CLR 2 0 内存模型的 6 条规则 http www bluebytesoftware com blog 2007 11 10 CLR20MemoryModel aspx 这是实际的实现 而不是任何 ECMA 标准
  • 如何在 iPhone UILabel 中设置字距调整

    我正在开发一个 iPhone 应用程序 我想在 UILabel 中设置字距调整 我写的代码 可能围绕kCTKernAttributeName 似乎有错误 我该如何解决这个问题 NSMutableAttributedString attStr