• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How to change a font dynamically?

Explorer ,
Apr 10, 2017 Apr 10, 2017

Copy link to clipboard

Copied

At a dialog I try to change a font dynamically. "Dynamically" means, changes of the font name and size by changes of related controls should affect the font of a static text (or edit field) immediatetly – not only if the values are stored at preferences and read by a subsequent call of the dialog. A test script:

***

local LrDialogs = import 'LrDialogs'

local LrView = import 'LrView'

local LrFunctionContext = import 'LrFunctionContext'

local LrBinding = import 'LrBinding'

local LrPrefs = import 'LrPrefs'

local LrLogger = import 'LrLogger'

local log = LrLogger('TestLogger')

log:enable('logfile')

local f = LrView.osFactory()

local bind = LrView.bind

local prefs = LrPrefs.prefsForPlugin()

local text = 'Lorem ipsum dolor sit amet, consectetur adipisici elit, ...'

LrFunctionContext.callWithContext ('showPreview', function(context)

  local props = LrBinding.makePropertyTable(context)

  props.font_name = prefs.fontName or 'Courier'

  props.font_size = prefs.fontSize or 12

  log:trace('Read: ' .. props.font_name .. '/' .. props.font_size)

  local previewText = f:static_text {

    title = text,

    font = {

      name = props.font_name,

      size = tonumber(props.font_size)

    },

  }

  local function updateFont()

    previewText.font = {

      name = props.font_name,

      size = tonumber(props.font_size),

    }

    log:trace('Change: ' .. props.font_name .. '/' .. props.font_size)

  end

  props:addObserver('font_name', updateFont)

  props:addObserver('font_size', updateFont)

  local contents = f:column {

    spacing = f:control_spacing(),

    bind_to_object = props,

    previewText, -- defined above

    f:separator { fill_horizontal = 1 },

    f:row {

      f:static_text { title = 'Font Name:' },

      f:combo_box {

        value = bind 'font_name',

        width_in_chars = 10,

        immediate = true,

        items = {

          'Courier', -- Mac

          'Courier New', -- Win

          'Lucida Console', -- Win

          'Monaco', -- Mac

        },

      },

      f:static_text { title = 'Font Size:' },

      f:combo_box {

        value = bind 'font_size',

        width_in_chars = 2,

        immediate = true,

        precision = 0,

        items = { 10, 12, 14, 16, },

      },

    },

  }

  LrDialogs.presentModalDialog({

    title = 'Font Change Test',

    contents = contents,

  })

  log:trace('Save: ' .. props.font_name .. '/' .. props.font_size)

  prefs.fontName = props.font_name

  prefs.fontSize = props.font_size

end)

***

The problem is: It works on macOS, not on Windows.

TOPICS
SDK

Views

562

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 11, 2017 Apr 11, 2017

Copy link to clipboard

Copied

LATEST

The problem is: It works on macOS, not on Windows.

Some of these restrictions are documented, many not.  (For example, "background_color" of scrolled_view() cannot be dynamically changed on Windows.)

I worked around the restriction by using two overlapping static_text() controls, one with regular font and one with bold, with the visibility of each dynamically controlled.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines