现在,NSAttributedString在iOS 6中可用.出于布局目的,我想知道如何在固定宽度下计算NSAttributedString所需的高度.我正在寻找与NSString相当的东西,- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size但是对于NSAttributedString.
要计算NSAttributedStrings的绘图大小,有两种方法可用:
- (CGSize)size 不能使用,因为它没有考虑任何宽度.- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options context:(NSStringDrawingContext *)context,但不知怎的,它没有给我正确的高度.我觉得这个方法很麻烦.如果我运行以下代码,它bounding size: 572.324951, 19.000000会让我忽略200的给定宽度.它应该给我一些像100的高度.
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] init];
NSDictionary *attributes = @{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue" size:15], NSForegroundColorAttributeName : [UIColor blueColor]};
[attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@"Attributed String\n" attributes:attributes]];
[attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@"Attributed String\n" attributes:attributes]];
[attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@"Attributed String\n" attributes:attributes]];
[attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@"Attributed String\n" attributes:attributes]];
[attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@"Attributed String\n" attributes:attributes]];
CGRect frame = [attributedString boundingRectWithSize:CGSizeMake(200, 1000) options:0 … 根据Swift 3中Data的文档,我可以使用一个初始化程序从UnsafeRawPointer创建数据.实际上我需要的是相反的.我有一个数据,我想创建一个指向数据字节的UnsafeRawPointer.这就是我现在正在做的事情:
1. let data = <from some where>
2. let unsafePointer = UnsafeMutablePointer<UInt8>.allocate(capacity: data.count)
3. unsafePointer.initialize(to: 0, count: data.count) // is this necessary?
4. data.copyBytes(to: unsafePointer, count: data.count)
5. let unsafeRawPointer = unsafePointer.deinitialize() // this is of the type UnsafeMutalbleRawPointer, and I can use it where UnsafeRawPointer is needed.
Run Code Online (Sandbox Code Playgroud)
我确认此代码适用于Xcode Playground.代码甚至可以在没有行号3的情况下工作.我不确定有没有线路的区别.无论如何,我的问题是,我做得对吗?有更简单的方法吗?
我正在阅读关于列表monad的在线Haskell书.在本书中,列表monad的定义如下:
instance Monad [] where
return x = [x]
xs >>= f = concat (map f xs)
fail _ = []
Run Code Online (Sandbox Code Playgroud)
然后有一个列表monad用法的例子,如下所示:
Prelude> [1,2] >>= \n -> ['a','b'] >>= \ch -> return (n,ch)
[(1,'a'),(1,'b'),(2,'a'),(2,'b')]
Run Code Online (Sandbox Code Playgroud)
我是Haskell的新手,关于这个例子的问题是,为什么变量'n'在lambda表达式中可用return (n,ch).n在另一个lambda表达式中定义,我不明白为什么在一个lambda表达式中定义的变量在后续的lambda表达式中可用.我尝试根据列表monad定义转换下面的示例:
Prelude> concat (map (\ch -> return (n, ch)) (concat (map (\n -> ['a', 'b']) [1, 2])))
<interactive>:32:29: error: Variable not in scope: n
Run Code Online (Sandbox Code Playgroud)
但正如您所看到的,我得到一个错误,说该变量n在另一个lambda表达式的范围内不可用.也许这本书只提出了列表monad定义的简化版本?
我有UITableView几个UITableViewCells在里面.因为我只有几个单元格,所以表格视图的某个区域没有被单元格覆盖.我想在点击空白区域时做点什么.
我尝试UITapGestureRecognizer在表视图上添加一个.它检测到空区域的敲击,但随后细胞无法响应敲击.我尝试在表视图的超级视图上添加点击手势识别器,但结果是一样的.
必须有办法做到这一点,但我还不能弄明白.有没有办法实现我想做的事情?
这是我的 Python 代码:
import pytz
from datetime import datetime
tz = pytz.timezone('US/Pacific')
now_local = datetime.now().replace(tzinfo=tz)
print("now_local: {}".format(now_local))
Run Code Online (Sandbox Code Playgroud)
它打印此输出:
now_local: 2018-11-13 12:06:03.255983-07:53
Run Code Online (Sandbox Code Playgroud)
这很奇怪,因为我认为时区偏移量应该是-08:00而不是-07:53. 我很确定美国太平洋的时区偏移是 8 小时。
我错过了什么吗?
我使用的是 Python 版本 2.7.14 和 Pytz 版本 2018.4
ios ×2
bounding-box ×1
haskell ×1
list ×1
monads ×1
objective-c ×1
python ×1
pytz ×1
swift ×1
swift3 ×1
timezone ×1
uitableview ×1