xcode 如何在 iOS Swift 项目中实现向下箭头/插入符号(如在 iOS 9 音乐应用程序中)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35602506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:34:40  来源:igfitidea点击:

How to implement a down arrow / caret (like in the iOS 9 Music app) in an iOS Swift project?

iosxcodeswiftuikitinterface-builder

提问by Georg

I am looking for the best way to create a down arrow (caret?) at the right hand side of a UILabel in a view that is centered in a UINavigationItem.

我正在寻找在 UINavigationItem 居中的视图中在 UILabel 的右侧创建向下箭头(插入符号?)的最佳方法。

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

The whole thing should look like the down arow in the iOS 9 Music app. The text of the label will change during runtime and the title/text of the label including the arrow should be centered like in this screenshot:

整个事情应该看起来像 iOS 9 音乐应用程序中的向下行。标签的文本会在运行时发生变化,包括箭头在内的标签的标题/文本应该像这个截图一样居中:

Screenshot of Music app on IOS 9.2

IOS 9.2 音乐应用截图

Currently I append a \u{25BE}character at the end of the text like this:

目前我\u{25BE}在文本末尾附加一个字符,如下所示:

self.lblSelectedAlbum.text = self.lblSelectedAlbum.text! + " \u{25BE}"

But this has several negative effects:

但这有几个负面影响:

  1. It looks a cluncky
  2. If the label text is too long, the down arrow will disappear
  1. 它看起来笨重
  2. 如果标签文字太长,向下箭头会消失

Any ideas on how to implement this best? Thanks!

关于如何最好地实现这一点的任何想法?谢谢!

采纳答案by Peter DeWeese

I suggest using an external font in attributed text. One example: Font Awesome's angle-down and chevron-down.

我建议在属性文本中使用外部字体。一个例子:Font Awesome的角度向下和人字形向下。

enter image description here

在此处输入图片说明

If you switch to a text view, you can set insets like

如果您切换到文本视图,您可以设置像

myTextView.edgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)]; //Replace 10 with the actual width that will center it.

You can also subclass UILabel to modify how it draws as in thisexample.

您还可以子类化 UILabel 以修改它的绘制方式,如例所示。

回答by Juanpablo Macias

Here is the unicode: \u{2304}

这是 unicode:\u{2304}

In context it looks like this. Avoid the hassle of downloading a new font.

在上下文中它看起来像这样。避免下载新字体的麻烦。

let workout = NSMutableAttributedString(string: "Workout", attributes: [NSFontAttributeName: UIFont(name: "Verdana", size: 14)!])
let arrow = NSMutableAttributedString(string: "\u{2304}", attributes: [NSFontAttributeName: UIFont(name: "Verdana", size: 37)!])

let combination = NSMutableAttributedString()
combination.append(workout)
combination.append(arrow)

sender.setAttributedTitle(combination, for: .normal)

回答by Ilesh P

One another way to user button and set text which you want to set and and the text you set button image to down arrow. if need to set position of button image and button title than use EdgesInset of image and title. I hope this is helpful to you. Thanks

用户按钮并设置要设置的文本以及将按钮图像设置为向下箭头的文本的另一种方法。如果需要设置按钮图像和按钮标题的位置而不是使用图像和标题的 EdgesInset。我希望这对你有帮助。谢谢