菜鸟教程小白 发表于 2022-12-12 03:57:21

ios - iphone notes.app 像下划线


                                            <p><p>我已经搜索了很多类似的问题,我决定在这里问这个问题,如果已经问过,我找不到它,抱歉。 </p>

<p>我发现:<a href="https://stackoverflow.com/questions/2615841/uitextview-like-iphone-notes-application" rel="noreferrer noopener nofollow">UITextView like iPhone Notes application</a>重定向到这里:<a href="http://www.cocoanetics.com/2010/03/stuff-you-learn-from-reverse-engineering-notes-app/" rel="noreferrer noopener nofollow">http://www.cocoanetics.com/2010/03/stuff-you-learn-from-reverse-engineering-notes-app/</a>但是在逆向工程中没有完整的代码示例!? </p>

<p>我正在为 iphone 创建一个应用程序,该应用程序存储并允许人们在 iphone 中输入他们的文本,如 notes.app。据我所知,我需要在文本下划线,我需要在 uitextview 中进行,这是 uiscrollview 的子类 .. </p>

<p>如果有人有任何建议,我会很高兴.. </p>

<p>谢谢..</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>当我需要使用 UILabel 执行此操作时,我创建了 UIView 的子类并自己实现了自定义绘图。它非常详细,但并不那么复杂。我最终传递了处理粗体(<b> 和</b>)和换行符(<br>)格式的类似HTML 的语法。我认为您可以使用 (< u > 和 </u >) 对下划线做同样的事情。 </p>

<p>基本上,我的技术是使用类似 HTML 的标记(首先在“br”标签上,然后在“b”标签上)拆分字符串,然后将每个子字符串拆分为“单词”,然后处理测量和单独绘制每个单词。如果一个单词不适合当前行,我会换行到下一行。它并不完美,它不能处理很多场景(例如,新的行标签不能放在粗体标签内),但它符合我的意图。 </p>

<p>不幸的是,我不知道为什么某些(大多数/全部?)iOS 基本文本显示控件没有更好地支持文本格式。 </p>

<hr/>

<p><em><strong>编辑/更新:</strong></em><br/>
我将继续添加我编写的 UIView 子类(UILabel 的替换)。使用风险自负! :-)</p>

<p><code>MMFormattedTextView.h</code></p>

<pre><code>#import &lt;UIKit/UIKit.h&gt;&lt;br&gt;
@interface MMFormattedTextView : UIView {
    int InsetLeft;
    int InsetTop;
    NSString *LabelText;
    UIFont *LabelFont;
}
@property (assign, nonatomic) int InsetLeft;
@property (assign, nonatomic) int InsetTop;
@property (strong, nonatomic) NSString *LabelText;
@property (strong, nonatomic) UIFont *LabelFont;
- (NSInteger)numberOfLinesForRect:(CGRect)rect;
@end
</code></pre>

<p><code>MMFormattedTextView.m</code></p>

<pre><code>#import &#34;MMFormattedTextView.h&#34;
@implementation MMFormattedTextView
@synthesize InsetLeft;
@synthesize InsetTop;
@synthesize LabelFont;
@synthesize LabelText;
// LIMITATION: Each bolded section must reside IN BETWEEN &lt;br&gt; tags; it MAY NOT span &lt;br&gt; tags!!!!
- (void)drawRect:(CGRect)rect {
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextClearRect(ctx, rect);
    // Sets up the first position, which is 1 line &#34;off the top&#34;,
    // adjusted so that the text will be centered when it&#39;s all drawn
    CGFloat howManyLinesWouldFit = rect.size.height / [ lineHeight];
    NSInteger howManyLinesDoWeHave = ;
    CGFloat lineOffset = (howManyLinesWouldFit - howManyLinesDoWeHave) / 2.0;
    CGPoint topLeft = CGPointMake(, - [ lineHeight] + (lineOffset * [ lineHeight]));
    // Split the text into hard-split lines (actual &lt;br&gt; tags in the text)
    NSArray *lines = [ componentsSeparatedByString:@&#34;&lt;br&gt;&#34;];
    // Iterate through each hard-coded line
    for (NSString *line in lines) {
      // Iterate to the next line
      topLeft = CGPointMake(, topLeft.y + [ lineHeight]);
      NSArray *pieces = ;
      BOOL bold = YES;
      for (NSString *piece in pieces) {
            bold = !bold;
            UIFont *fontToUse;
            if (bold) {
                fontToUse = pointSize]];
            } else {
                fontToUse = pointSize]];
            }
            NSArray *words = ;
            for (NSString *word in words) {
                if () continue;
                NSString *wordWithSpace = ;
                CGSize wordSize = ;
                if ((topLeft.x + wordSize.width) &gt; (rect.size.width - )) {
                  // This runs off this line, so go to the next line
                  topLeft = CGPointMake(, topLeft.y + [ lineHeight]);
                }
                ;
                topLeft = CGPointMake(topLeft.x + wordSize.width, topLeft.y);
            }
      }
    }
}
- (NSInteger)numberOfLinesForRect:(CGRect)rect {
    int retVal = 0;
    int left = ;
    NSArray *lines = [ componentsSeparatedByString:@&#34;&lt;br&gt;&#34;];
    // Iterate through each hard-coded line
    for (NSString *line in lines) {
      // Iterate to the next line
      retVal = retVal + 1;
      left = ;
      NSArray *pieces = ;
      BOOL bold = YES;
      for (NSString *piece in pieces) {
            bold = !bold;
            UIFont *fontToUse;
            if (bold) {
                fontToUse = pointSize]];
            } else {
                fontToUse = pointSize]];
            }
            NSArray *words = ;
            for (NSString *word in words) {
                if () continue;
                NSString *wordWithSpace = ;
                CGSize wordSize = ;
                if ((left + wordSize.width) &gt; (rect.size.width - )) {
                  // This runs off this line, so go to the next line
                  retVal = retVal + 1;
                  left = ;
                }
                left = left + wordSize.width;
            }
      }
    }
    return retVal;
}
@end
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - iphone notes.app 像下划线,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/10375592/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/10375592/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - iphone notes.app 像下划线