- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个 UITableView,其单元格包含一个 UILabel。UILabel 有一个自定义的 UIEdgeInset。我将 UILabel 子类化并像这样设置 UIEdgeInsets:
override func drawText(in rect: CGRect) {
super.drawText(in: UIEdgeInsetsInsetRect(rect, insets))
}
override var intrinsicContentSize: CGSize {
var contentSize = super.intrinsicContentSize
contentSize.width += leftInset + rightInset
contentSize.height += topInset + bottomInset
return contentSize
}
但是当我在 UILabel 中有更多行时,标签有时会被截断。我已经将行高配置为 UITableViewAutomaticDimension 并设置了 estimatedRowHeight。约束也很好。问题似乎出在我设置 UIEdgeInsets 时,因为如果我不对其进行自定义,它就可以正常工作。
可能我应该告诉单元格在设置插图后更新约束,但到目前为止我不能这样做。
Storyboard中添加的约束。 Bottom
、Top
、Leading
和 Trailing
与父 View (UITableViewCell) 相关。所有常量设置为 0。
在cellForRowAtIndexPath
中的代码如下:
let cell = tableView.dequeueReusableCell(withIdentifier: "AnswersCell", for: indexPath) as! AnswerCell
cell.answerLabel.text = alternatives[indexPath.row]
return cell
最佳答案
从 Storyboard的属性检查器部分设置 UILable 的这两个属性
或者您也可以通过代码设置这些属性。
self.answerLabel.numberOfLines = 0
self.answerLabel.lineBreakMode = .byWordWrapping
并将下面的代码放在你的 UILable 的子类中
class CustomLabel: UILabel {
@IBInspectable var topInset: CGFloat = 5.0
@IBInspectable var bottomInset: CGFloat = 5.0
@IBInspectable var leftInset: CGFloat = 20.0
@IBInspectable var rightInset: CGFloat = 5.0
override func drawText(in rect: CGRect) {
let insets = UIEdgeInsets(top: topInset, left: leftInset, bottom: bottomInset, right: rightInset)
super.drawText(in: UIEdgeInsetsInsetRect(rect, insets))
}
override var intrinsicContentSize: CGSize {
get {
var contentSize = super.intrinsicContentSize
contentSize.height += topInset + bottomInset
contentSize.width += leftInset + rightInset
return contentSize
}
}
试试这个。
希望它对你有用..
关于ios - 带有自定义 UIEdgeInsets 的 UILabel 在 UITableViewCell 中截断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41753078/
我希望按钮文本出现在按钮中间附近的 x 维度上。但是按钮文本仍然保留在 View 的最左侧,就好像 x 是 0 一样。我已确保 setTitleEdgeInsets 和 setContentHoriz
我尝试制作一个 UIButton ,图像位于文本上方,因此我使用以下代码: UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
这似乎是一个基本问题,但我正在 TableView 的顶部创建一个 UIEdgeInset,如下所示: self.tableView.contentInset = UIEdgeInsetsMake(4
我想在不实际增加其原始大小的情况下增加按钮触摸区域。 当涉及到 insets 时,Button 中有很多,但我找不到关于差异的明确文档 内容插入 标题插入 图像插图 所以后来我想试试内容插图。根据其他
因此,我查看了 Stack Overflow 上所有为 UIButton 中的 UIImages 添加边缘插图的项目,但没有一个起作用。当我构建项目时,图像只是通过按钮的边界设置 - 它没有设置为插图
我需要调整 iPhone X 的 View ,但我不知道 safeAreaInsets 何时初始化。根据文档, If the view is not currently installed in a
我一直在尝试为 UIEdgeInsets 中的变化设置动画.我已经尝试使用 UIView.animateWithDuration并且插图只是跳到最终值。有谁知道这是否是一个不能动画的属性?或者更好的是
UIEdgeInsets ? 1
我正在为 UITabBarItem 使用 XCode 系统图像(SF 符号)图片。我想删除 UITabBarItem我做过的标题。还要动UITabBarItem图像略有下降。 过去,当我不使用系统镜像
我希望 UICollectionView 的项目位于中心,因此我编写了以下代码: func collectionView(collectionView: UICollectionView, layou
我有一些内容要显示在 NMAMapView 的顶部。我想缩放和平移到特定的边界框,但我希望它尊重一种边缘插入,这样我的叠加内容就不会掩盖我希望在 map View 中看到的内容。 我看过here ,但
我有这个扩展: extension UIEdgeInsets { init(top: CGFloat = 0, left: CGFloat = 0, bottom: CGFloat = 0,
你好,我有 UIControllerView ,我用下面的代码设置了 UIEdgeInsets; self.automaticallyAdjustsScrollViewInsets = false
我正在尝试在 Objective-C 中创建一个 UIEdgeInsets 实例列表。因为它们不是 id 类型,所以我无法将它们添加到数组中。 我想做的是这样的: NSArray *contentIn
如何将 UIEdgeInsets(或其某些变体)应用于 CGImage?这是我最终要实现的目标,但是 CGImage 在转换为 CGImage 之前无法识别应用于 UIImage 的底部插图>. va
我有一个 CGRect ,我想用 UIEdgeInsets 调整它. 似乎可能有一个内置函数可以做到这一点。我找了一个 CGRectAdjustByInsets或与其他一些功能一起使用 CGRect…
我正在使用 iOS 5 中引入的 UIAppearance 为我的应用程序中的所有 BarButtonItems 设置自定义背景图像。 下面的代码是我用来设置后退按钮的代码,在纵向模式下效果很好。但是
我正在实现此方法以重新定位 UIScrollView 以显示被键盘弹出窗口遮挡的 UITextField: Move a view up only when the keyboard covers a
在绘制到屏幕外缓冲区时,我很难让 Image Insets 正常工作。 在 UIImage 上直接使用 resizableImageWithCapInsets: setImage: into a bu
我创建了一个屏幕,其中有一个全屏 UITableView,我为其设置了 UIEdgeInsets,我的配置如下: categoriesTableView.contentInset = UIEdgeIn
我是一名优秀的程序员,十分优秀!