iPhone 项目符号列表

发布于 2024-09-24 23:39:34 字数 124 浏览 2 评论 0原文

有没有办法在 iPhone 中制作项目符号列表?

如果您将项目符号列表复制并粘贴到 IB 中的 UITextView 中,那么它就会显示出来。有没有办法以编程方式执行此操作?

谢谢

汤姆

Is there any way to make a bullet point list in iphone?

If you copy and paste a bullet point list into a UITextView in IB then it shows up. Is there anyway to do this programatically?

Thank you

Tom

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眼角的笑意。 2024-10-01 23:39:34

“项目符号”字符位于 Unicode 代码点 U+2022。您可以在带有 @"\u2022"[NSString stringWithFormat:@"%C", 0x2022] 的字符串中使用它。

“换行”字符位于 Unicode 代码点 U+000A,并用作 UIKit 的换行符。您可以在带有 @"\n" 的字符串中使用它。

例如,如果您有一个字符串数组,您可以使用如下内容创建项目符号列表:

NSArray * items = ...;
NSMutableString * bulletList = [NSMutableString stringWithCapacity:items.count*30];
for (NSString * s in items)
{
  [bulletList appendFormat:@"\u2022 %@\n", s];
}
textView.text = bulletList;

它不会像“正确的”文字处理器那样缩进行。如果您的列表项包含换行符,就会发生“坏事”(但您期望什么?)。

(Apple 不保证“\uXXXX”转义在 NSString 文字中起作用,但实际上,如果您使用 Apple 的编译器,它们会起作用。)

The "bullet" character is at Unicode code point U+2022. You can use it in a string with @"\u2022" or [NSString stringWithFormat:@"%C", 0x2022].

The "line feed" character is at Unicode code point U+000A, and is used as UIKit's newline character. You can use it in a string with @"\n".

For example, if you had an array of strings, you could make a bulleted list with something like this:

NSArray * items = ...;
NSMutableString * bulletList = [NSMutableString stringWithCapacity:items.count*30];
for (NSString * s in items)
{
  [bulletList appendFormat:@"\u2022 %@\n", s];
}
textView.text = bulletList;

It won't indent lines like a "proper" word processor. "Bad things" will happen if your list items include newline characters (but what did you expect?).

(Apple doesn't guarantee that "\uXXXX" escapes work in NSString literals, but in practice they do if you use Apple's compiler.)

雪化雨蝶 2024-10-01 23:39:34

据我所知,在 iPhone 上实现此目的(以及几乎任何其他格式化的富文本)的唯一方法是使用 UIWebView 并插入如下 HTML 代码:

<ul>
     <li>Bullet</li>
</ul>

In response to your comment, UIWebViews can be "beautified" by以下代码:

for(UIView* v in webView.subviews){
    if([v isKindOfClass:[UIScrollView class] ]){

        //disable bouncing
        UIScrollView* sv = (UIScrollView*) v;
        sv.alwaysBounceVertical = NO;
        sv.alwaysBounceHorizontal = NO;

        //disable scroll-shadows
        for (UIView *subView in [sv subviews])
            if ([[[subView class] description] isEqualToString:@"UIImageView"])
                subView.hidden = YES;
    }
}

我还没有提交这个,但我想它应该是“AppStore 安全的”。

As far as I know the only way to achieve this (and almost any other formatted rich-text) on the iPhone is to use a UIWebView and insert HTML-Code like this:

<ul>
     <li>Bullet</li>
</ul>

In response to your comment, UIWebViews can be "beautified" by the following peace of code:

for(UIView* v in webView.subviews){
    if([v isKindOfClass:[UIScrollView class] ]){

        //disable bouncing
        UIScrollView* sv = (UIScrollView*) v;
        sv.alwaysBounceVertical = NO;
        sv.alwaysBounceHorizontal = NO;

        //disable scroll-shadows
        for (UIView *subView in [sv subviews])
            if ([[[subView class] description] isEqualToString:@"UIImageView"])
                subView.hidden = YES;
    }
}

I haven't submitted this yet but I guess it should be "AppStore safe".

诗笺 2024-10-01 23:39:34

Swift-5

只需用 Swift 编写 @tc 答案

let items: [String] = ["Point-1", "Point-2", "Point-3"]
var bulleLists: [String] = []
for item in items {
     bulleLists.append("\u{2022}" + item + "\n")
}
textview.text = bulleLists.joined()

Swift-5

Just writing the @tc answer in Swift

let items: [String] = ["Point-1", "Point-2", "Point-3"]
var bulleLists: [String] = []
for item in items {
     bulleLists.append("\u{2022}" + item + "\n")
}
textview.text = bulleLists.joined()
陈年往事 2024-10-01 23:39:34
var bulletLists: [String] = ["Apple", "Banana"]
let bullet = "\u{2022} " // your bullet style
bulletLists = bulletLists.map { bullet + $0 }
yourTextView.adjustsFontForContentSizeCategory = true

// set font attributes
var attributes = [NSAttributedString.Key: Any]()
attributes[.font] = remarkTextView.font
attributes[.foregroundColor] = UIColor(.enterpriseGray)

// calculate indent size
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.headIndent = (bullet as NSString).size(withAttributes: attributes).width
attributes[.paragraphStyle] = paragraphStyle

// add attributes to your textView
yourTextView.attributedText = NSAttributedString(string: bulletLists.joined(separator: "\n"), attributes: attributes)

结果:

• Apple
• Banana
var bulletLists: [String] = ["Apple", "Banana"]
let bullet = "\u{2022} " // your bullet style
bulletLists = bulletLists.map { bullet + $0 }
yourTextView.adjustsFontForContentSizeCategory = true

// set font attributes
var attributes = [NSAttributedString.Key: Any]()
attributes[.font] = remarkTextView.font
attributes[.foregroundColor] = UIColor(.enterpriseGray)

// calculate indent size
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.headIndent = (bullet as NSString).size(withAttributes: attributes).width
attributes[.paragraphStyle] = paragraphStyle

// add attributes to your textView
yourTextView.attributedText = NSAttributedString(string: bulletLists.joined(separator: "\n"), attributes: attributes)

result:

• Apple
• Banana
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文