[MacRuby-devel] calculate table row height

niedhui niedhui at gmail.com
Mon Jun 21 21:01:36 PDT 2010


Hello,I'm new to cocoa and macruby,
  I want to start a tweetie-like app to learn macruby & cocoa, when display status list,I use NSTableView,and I must calculate the row height with `def tableView(table,heightOfRow:row);end` myself ,right? After googled  a while,I found the way  ,like this:


framework 'cocoa'
class String
  
  def height_with_width(width=100)
  
	attributes = {}
	attributes[NSFontAttributeName.to_sym] = NSFont.fontWithName("Lucida Grande", size:12)
	attributes[NSParagraphStyleAttributeName.to_sym] = NSParagraphStyle.defaultParagraphStyle
  
        text_storage = NSTextStorage.alloc.initWithString(self)
	text_container = NSTextContainer.alloc.initWithContainerSize([width,1000])
	layout_manager = NSLayoutManager.alloc.init
	layout_manager.addTextContainer(text_container)
	text_storage.addLayoutManager(layout_manager)
	text_storage.addAttributes(attributes, range:NSMakeRange(0, text_storage.length))
	text_container.setLineFragmentPadding(0.0)
	layout_manager.glyphRangeForTextContainer(text_container)
	layout_manager.usedRectForTextContainer(text_container).size.height
  end
end


puts "a".height_with_width    #15
puts "b".height_with_width    #15
puts "ab".height_with_width   #15
puts "中".height_with_width    #18
puts "ak中".height_with_width  #19
puts "ak中".height_with_width(1)  #15+15+18


because the chinese characters, the height of a line is diffence, I want it to be same ,like all return 19 , How Can I Fix This ? HELP!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-devel/attachments/20100622/3636d04b/attachment.html>


More information about the MacRuby-devel mailing list