uitextview背景色_UITextView中带有圆角的背景

2023-05-16

uitextview背景色

I have had the opportunity to learn so many things while working on Proton. Just as much as I like sharing my code, I also enjoy talking about the challenges and the wins. This article focuses on yet another feature in Proton which, I believe, should have been available out of the box in iOS. If you are new here and before diving into the details, would like to know more about Proton, Building a native editor for iOS is a good place to start.

在Proton工作期间,我有机会学习了很多东西。 就像我喜欢共享代码一样,我也喜欢谈论挑战和胜利。 本文着重介绍Proton的另一个功能,我认为应该在iOS中立即可用。 如果您是这里的新手,在开始探索细节之前,想了解有关Proton的更多信息,那么为iOS构建本机编辑器是一个不错的起点。

带有背景的文字 (Text with background)

Often there are times when we want to add background to the text in a text view. The most simplest, and probably best, approach is to use backgroundColor attribute. You can provide any UIColor to a given range, and iOS takes care of rendering that color in the background of the text.

通常,有时候我们想在文本视图中为文本添加背景。 最简单,也可能最好的方法是使用backgroundColor属性。 您可以提供给定范围内的任何UIColor ,iOS负责在文本背景中呈现该颜色。

The only code you need is to add attribute backgroundColor: UIColor.cyan to the list of NSAttributedString.Key in the attributedText property of UITextView. While this works for most cases, it may feel too vanilla. You might want a fancier formatting like adding shadows or borders. At the very least, it is very likely that you would want rounded corners to the background. Unfortunately, none of these properties may be modified by just using attributes.

您唯一需要的代码是将属性backgroundColor: UIColor.cyan添加到UITextViewattributedText属性中的NSAttributedString.Key列表中。 尽管这在大多数情况下都有效,但可能会感觉太过香草。 您可能希望使用更高级的格式,例如添加阴影或边框。 至少,您很可能希望对背景进行圆角处理。 不幸的是,仅使用属性就不能修改这些属性。

更好的背景 (Better backgrounds)

One of the principles I am following with Proton is to provide all that, we as developers, might want to customize and yet keep those customizations natural. What I mean is that all the customizations that you might want to do, should ideally not take anything more than adding an attribute with required information. For e.g. if you want to add lists to a UITextView, it should be as simple as adding a .listItem attribute to the range of text. You can read about my adventures with lists in Lists in UITextView. With Proton, you can add an attribute, very similar to backgroundColor, and achieve the following:

我作为Proton遵循的原则之一就是为开发人员提供所有可能想要自定义但仍保持自然自定义的功能。 我的意思是,理想情况下,您可能要进行的所有自定义操作都不应只添加带有必需信息的属性。 例如,如果您要将列表添加到UITextView,则应像向文本范围添加.listItem属性一样简单。 您可以在UITextView的“列表”中阅读有关我的冒险经历的列表。 使用Proton ,您可以添加一个非常类似于backgroundColor的属性,并实现以下目的:

Background with rounded corners
圆角的背景
Rounded corners with shadows
圆角与阴影
Background with borders only
仅带边框的背景
Shadow, border and rounded corners
阴影,边框和圆角

The only code you need to add is:

您需要添加的唯一代码是:

let style = BackgroundStyle(color: .green, cornerRadius: 5, 
border: BorderStyle(lineWidth: 1, color: UIColor.blue),
shadow: ShadowStyle(color: .gray, offset: CGSize(width: 2, height: 2), blur: 3))editor.addAttribute(.backgroundStyle, value: style, at: editor.selectedRange)

editor is the Proton counterpart for UITextView. Besides other features, it provides simple API to manipulate attributes.

editorUITextView的Proton对应项。 除了其他功能,它还提供了简单的API来操纵属性。

引擎盖下 (Under the hood)

If you want to understand how this has been achieved in Proton, please read on.

如果您想了解Proton是如何实现的,请继续阅读。

The layout and drawing of text is managed by NSLayoutManager. It provides two functions that can be overridden to achieve the desired behaviour:

文本的布局和图形由NSLayoutManager管理。 它提供了两个可以重写以实现所需行为的功能:

初始方法 (Initial approach)

The first approach I tried was using fillBackgroundRectArray. This function provides a finer control over drawing the background for the ranges with .backgroundColor attribute. This function gets invoked with all the rectangles defining the range with background color.

我尝试的第一种方法是使用fillBackgroundRectArray 。 此功能可更好地控制使用.backgroundColor属性绘制背景范围的背景。 所有定义背景颜色范围的矩形均会调用此函数。

Drawing background with rounded corners
带有圆角的绘图背景

This approach works, but depending upon how content is added, fillBackgroundRectArray may be called multiple times even for the text that is in the same line. This results in rendering rounded corners based on words instead of lines of text:

这种方法有效,但是取决于内容的添加方式,即使对于同一行中的文本,也可以多次调用fillBackgroundRectArray 。 这导致基于单词而不是文本行渲染圆角:

If the content is pasted instead of being typed, the background is drawn as expected with no visible rounding per word as we see above.

如果粘贴内容而不是键入内容,则会如预期那样绘制背景,并且每个单词都没有可见的舍入,如我们上面看到的。

最终方法 (Final approach)

Spending a little more time with NSLayoutManager, I figured out that using drawBackground is a better option. Using similar code as above, you can achieve better results.

我花了更多的时间在NSLayoutManager ,我发现使用drawBackground是更好的选择。 使用上面类似的代码,可以获得更好的结果。

In Proton, I added additional logic to calculate the rounded corners based on each of the lines that are overlapping and also used a trick that ensures that when there are borders, the borders are only drawn on outer edges of the rectangles with background.

在Proton中 ,我添加了其他逻辑以根据重叠的每条线计算圆角,还使用了一种技巧来确保当有边界时,仅在具有背景的矩形的外边缘上绘制边界。

The initial version with all the selected range looked like the following where the corners are rounded at outer edges, but there is still bottom line of the first rectangle that is causing this to appear as two separate background rectangles.

带有所有选定范围的初始版本如下所示,其中的角在外边缘处是圆角的,但是第一个矩形的底线仍然导致其显示为两个单独的背景矩形。

Using some simple logic, I was able to calculate the overlapping line, which is shown in yellow color below:

使用一些简单的逻辑,我能够计算出重叠线,该重叠线以黄色显示如下:

Once the overlapping line is identified, the only other thing that is left is to use the same color for the line as that for the background, so that the line disappears:

一旦识别出重叠的线,剩下的唯一一件事就是对该线使用与背景相同的颜色,以便该线消失:

还有一件事 (One more thing)

With all the calculations that are put in place to draw background with rounded corners, it also helps drawing the background dynamically where it merges and splits the background based on the range of text having the backgroundStyle applied:

使用所有计算来绘制带有圆角的背景时,它还有助于动态绘制背景,并根据应用了backgroundStyle的文本范围在其中合并和拆分背景的地方:

I hope you enjoyed reading about this just as much as I enjoyed developing this feature in Proton.

希望您喜欢阅读此书,就像我喜欢在Proton中开发此功能一样。

翻译自: https://levelup.gitconnected.com/background-with-rounded-corners-in-uitextview-1c095c708d14

uitextview背景色

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

uitextview背景色_UITextView中带有圆角的背景 的相关文章

  • 具有两种文本对齐方式的属性文本

    有谁知道如何在一个字符串中实现两种不同的文本对齐方式 这就是我想要 textView 显示的内容 label value My code let txtView cell viewWithTag 77 as UITextView let l
  • 如何将UIImage插入到UITextView中

    我正在开发一个可编辑的笔记本类型项目 它随时由一些文本和图像组成 在UITextView如果我们将图像添加为子视图 则帧是固定的 但我有editable选项 所以我必须将图像另存为NSString格式为UITextView 但它应该在 ui
  • (iPhone) selectedRange 用于不可编辑的 UITextView (或其他点击处理方法?)

    我正在使用一个UITextView保存静态内容 中文意思是字符都是固定宽度的 我想让用户单击文本中的某个字符并显示该字符的字典信息 我知道所有与缺乏复制和粘贴相关的问题 但我希望有一种方法可以做到这一点 而无需等待 iPhone 3 0 固
  • 如何禁用 UITextView 中的垂直滚动?

    我只想禁用 UITextView 中的垂直滚动同时保持水平滚动 我在这里发现了一些类似的问题 但是 它们似乎都不适合我 我尝试过的是 func scrollViewDidScroll scrollView UIScrollView let
  • iOS 7 UITextView:重新打开应用程序后 nstextattachment 的大小变为 2 倍

    我正在使用 ios7 中的文本工具包构建一个笔记编辑器 早些时候 我在渲染自定义大小的 NSTextAttachment 时遇到了麻烦 因为它在很大程度上减慢了渲染速度 我通过缩放图像然后将它们添加到 textview 解决了这个问题 您可
  • 如何强制右对齐 UITextView 中的尾随空格?

    我有一个 UITextView 可以左对齐 右对齐或居中 我注意到 如果我右对齐 textView 则 textView 中任何文本行中的任何尾随空格都会被忽略 左对齐时 前导空格不会发生这种情况 从视觉上看 这就是发生的情况 用 来可视化
  • UITextView从底部开始

    我遇到了问题UI文本视图 我以前见过其他人也遇到过这个问题 但是每当我的应用程序在我的手机上加载时 所有UITextViews从文本的底部开始 我必须向上滚动 我已经尝试了多种解决方案 但我需要一个仅在情节提要上使用的解决方案 因为我有一些
  • 如何使用返回键关闭 UITextView 的键盘?

    想要改进这篇文章吗 提供此问题的详细答案 包括引用和解释为什么你的答案是正确的 不够详细的答案可能会被编辑或删除 In IB s library the introduction tells us that when the return
  • 从 Xcode 11.1 升级到 Xcode 11.2 后,应用程序因 _UITextLayoutView 崩溃

    从 Xcode 11 1 升级到 Xcode 11 2 后 我的应用程序崩溃了 由于未捕获的异常 NSInvalidUnarchiveOperationException 而终止应用程序 原因 无法实例化名为 UITextLayoutVie
  • UITextView beginOfDocument 返回 nil

    我必须获取包含在屏幕上的字符串的位置UITextView 为此 我尝试获取相应的 UITextPosition 问题是UITextView method beginningOfDocument回报nil 同样适用于endOfDocuemen
  • 检测开始和停止编辑 UITextView

    如何在进入 UITextView 用户点击以编辑它 和离开视图 用户点击以离开它 时调用一些代码 感谢任何帮助 http developer apple com library ios documentation uikit referen
  • UITableview 中的 UItextView,自动更正气泡不可见

    我有一个带有自定义单元格的 UITableView 每个单元格包含一个 UITextView 并且当用户在 TextView 中键入文本时 单元格会调整大小 我的问题是 当用户位于 TextView 自动更正第一行时 当前单元格中将看不到气
  • 向 UITextView 添加轮廓/描边

    我想将轮廓或描边添加到可编辑的UITextView用户键入的文本 就像模因一样 http t qkme me 3oi5rs jpg http t qkme me 3oi5rs jpg 我必须使用UITextView因为我需要多线支持 我已经
  • Swift 中基于内容的动态 UITextView 大小

    我不认为有人可以指导我在仍然使用自动布局的同时动态调整 UITextView 大小的正确方法 使用 Swift 就是这样 我尝试以编程方式调整底部约束 试图让 UITextView 拥抱内容 但我不知道如何获取 UITextView 内容的
  • UITextView 可以容纳多少文本?

    我有一个不可编辑的UITextView显示用户输入的文本 如果文本很长会发生什么 它会让我的应用程序崩溃吗 我应该对文本进行分页吗 iPhone 网络浏览器 如 Safari Chrome 或 Firefox 如何处理此问题 有没有类似的东
  • 如何从 Swift 3 中的文本视图中删除查找和共享

    我可以使用它删除剪切 复制 粘贴 选择 选择所有内容 override public func canPerformAction action Selector withSender sender Any gt Bool if action
  • 具有非规则形状的 NSTextContainer 示例?

    嗨 我正在使用新的TextKitiOS7 的 API 我正在尝试制作一个UITextView具有不规则形状 到目前为止 我在视图控制器中有 void loadView self view UIView alloc initWithFrame
  • sizeWithFont: constrainedToSize: with UITextView (有时) 没有创建正确的高度 - iPhone

    我使用以下代码来确定所需的高度UITextView正在添加到UITableViewCell 并确定每个的高度UITableViewCell 这在 90 的情况下有效 但某些传入数据 始终是相同的传入数据 会出现不足 如果我允许在UIText
  • 在 UITextView 上添加具有自定义间距和文本的图像

    I want to add Image on text view and want proper spacing as shown in screenshot 我尝试在文本视图上添加图像 但在根据要求放置文本时遇到问题 请向我提供有关同样的
  • 使用 swift 自动滚动 UITextView (IOS 应用程序)

    我似乎无法找到自动滚动的正确解决方案UITextView using Swift 对于我的应用程序 我从 BT 接收持续数据 并且我正在更新它UITextView 然而 当UITextView将到达最新更新的行 我将不得不手动滚动 我想要实

随机推荐